如何找到在GridView中的占位符内的动态控制(单选按钮) [英] How to find a dynamic control (radio button) inside a placeholder in gridview
问题描述
我有4列gridview的;在第1列,我添加的占位和其它3列是绑定列。在第1列我加入动态使用HTML code单选按钮由我只能选择一个单选按钮。它的工作很好,但问题是我无法找到被点击GridView控件的按钮外,当单选按钮控制。
请帮帮忙,我一直停留在这个问题上4天。
感谢您提前。
我用下面的code
.aspx文件
<表ID =form1的=服务器> < ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =FALSE
背景色=白BORDERCOLOR =#999999边框=实边框宽度=1像素
CELLPADDING =3前景色=黑网格=垂直
onrowdatabound =GridView1_RowDataBound
>
< AlternatingRowStyle背景色=#CCCCCC/>
<柱体和GT;
< ASP:的TemplateField的HeaderText =选择>
<&ItemTemplate中GT; < ASP:占位符ID =PH=服务器>< / ASP:占位符>
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:BoundField的的HeaderText =名的DataField =FNAME/>
< ASP:BoundField的的HeaderText =姓氏数据字段=LNAME/>
< ASP:BoundField的的HeaderText =EMAIL数据字段=EMAIL/>
< ASP:BoundField的的HeaderText =年龄数据字段=年龄/>
< /专栏>
< FooterStyle背景色=#CCCCCC/>
< HeaderStyle背景色=黑字体粗体=真前景色=白/>
< PagerStyle背景色=#999999前景色=黑HorizontalAlign =中心/>
< SelectedRowStyle背景色=#000099FONT-粗体=真前景色=白/>
< SortedAscendingCellStyle背景色=#F1F1F1/>
< SortedAscendingHeaderStyle背景色=#808080/>
< SortedDescendingCellStyle背景色=#CAC9C9/>
< SortedDescendingHeaderStyle背景色=#383838/>
< / ASP:GridView的>
< ASP:按钮的ID =btnSave文本=保存=服务器的onclick =btnSave_Click1/> < /表及GT;
code Behind文件
保护无效GridView1_RowCreated(对象发件人,GridViewRowEventArgs E)
{
如果(!e.Row.RowIndex = -1&放大器;&安培;!e.Row.DataItem = NULL)
{
占位符支架=(预留)e.Row.FindControl(PH);
单选RB =新的单选按钮();
rb.ID =rbSelect;
holder.Controls.Add(RB);
}
} 保护无效btnSave_Click1(对象发件人,EventArgs的发送)
{
的for(int i = 0; I< GridView1.Rows.Count;我++)
{ 占位符支架=(预留)GridView1.Rows [I] .Cells [0] .FindControl(PH);
单选RBTN = holder.FindControl(RB)作为单选按钮;
如果(rbtn.Checked ==真)
{
的Response.Write(<脚本>警报('Radiocheck')< / SCRIPT>中);
}
}
}
目前还不清楚为什么你需要创建动态单选
■在所有。这使得在此情况下(即使一个嵌套的转发
或 GridView控件
会更容易没有好处都只是更加困难)。
然而,
因为这触发只有 GridView控件
是数据绑定你不应该创建的RowDataBound
动态控件。但是,由于默认情况下的ViewState
已启用你不会的DataBind
它回发。在另一方面动态控件必须重新创建的上的每个回发
因此,在 RowCreated
创建它们是触发回发的每个。但请注意,你有没有的DataItem
有,因为它是空
在这个阶段(即使栅格(当然)将数据绑定)。
所以,你应该在 RowCreated
创建cynamic控制,但由的RowDataBound
加载它们,您也可以访问它们(FE通过的FindControl
)。
但不是添加HTML控制像<输入类型='无线电'
应创建并添加单选
有一个id。否则,你将不能晚接取,所以 holder.FindControl(RB)
将是空
,因为它不是一个服务器的控制。
下面是完整的修改code:
保护无效GridView1_RowCreated(对象发件人,GridViewRowEventArgs E)
{
如果(e.Row.RowType == DataControlRowType.DataRow)
{
占位符支架=(预留)e.Row.FindControl(PH);
VAR RB =新的单选按钮();
rb.ID =RbSample;
rb.Text =RB
holder.Controls.Add(RB);
}
}保护无效GridView1_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
如果(e.Row.RowType == DataControlRowType.DataRow)
{
VAR行=((DataRowView的)e.Row.DataItem).Row;
VAR RB =(单选)e.Row.FindControl(RbSample);
rb.Checked = row.Field<布尔>(SampleActive);
}
}
保护无效btnSave_Click1(对象发件人,EventArgs的发送)
{
的for(int i = 0; I< GridView1.Rows.Count;我++)
{
单选RBTN =(单选)GridView1.Rows [I] .FindControl(RbSample);
如果(rbtn.Checked)
{
ScriptManager.RegisterClientScriptBlock(这一点,this.GetType(),警戒,警报('Radiocheck');,真正的);
}
}
}
I have gridview with 4 columns; in column 1 I added a place holder and other 3 columns are boundfields. In column 1 I'm adding radio buttons dynamically using html code by which I am able to select only one radio button. It's working well, but the problem is I am unable to find the radio button control when a button outside of the gridview is clicked.
Please help, I've been stuck on this problem for 4 days.
Thank you in advance.
I used the following code
.aspx File
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
CellPadding="3" ForeColor="Black" GridLines="Vertical"
onrowdatabound="GridView1_RowDataBound"
>
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="FIRST NAME" DataField="FNAME"/>
<asp:BoundField HeaderText="LAST NAME" DataField="LNAME"/>
<asp:BoundField HeaderText="EMAIL" DataField="EMAIL"/>
<asp:BoundField HeaderText="AGE" DataField="AGE"/>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
<asp:Button ID="btnSave" Text="Save" runat="server" onclick="btnSave_Click1" />
</form>
Code Behind file
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1 && e.Row.DataItem != null)
{
PlaceHolder holder = (PlaceHolder)e.Row.FindControl("ph");
RadioButton rb = new RadioButton();
rb.ID = "rbSelect";
holder.Controls.Add(rb);
}
}
protected void btnSave_Click1(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
PlaceHolder holder = (PlaceHolder)GridView1.Rows[i].Cells[0].FindControl("ph");
RadioButton rbtn = holder.FindControl("rb") as RadioButton;
if (rbtn.Checked == true)
{
Response.Write("<Script>alert('Radiocheck')</Script>");
}
}
}
It's not clear why you need to create dynamic RadioButton
s at all. That makes all just more difficult without a benefit in this case(even if, a nested Repeater
or GridView
would be easier).
However,
you should not create dynamic controls in RowDataBound
since that is triggered only if the GridView
was databound. But since by default ViewState
is enabled you wouldn't DataBind
it on postbacks. Dynamic controls on the other hand must be recreated on every postback.
Therefore create them in RowCreated
which is triggered on every postback. But note that you have (of course) no DataItem
there since it is null
at this stage(even if the grid will be databound).
So you should create cynamic controls in RowCreated
but load them from RowDataBound
where you can also access them (f.e. via FindControl
).
But instead of adding html-control like <input type='radio'
you should create and add a RadioButton
with an id. Otherwise you won't be able to acess it later, so holder.FindControl("rb")
will, be null
since it's not a server-control.
Here's the complete modified code:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
PlaceHolder holder = (PlaceHolder)e.Row.FindControl("ph");
var rb = new RadioButton();
rb.ID = "RbSample";
rb.Text = "rb";
holder.Controls.Add(rb);
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var row = ((DataRowView)e.Row.DataItem).Row;
var rb = (RadioButton)e.Row.FindControl("RbSample");
rb.Checked = row.Field<bool>("SampleActive");
}
}
protected void btnSave_Click1(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
RadioButton rbtn = (RadioButton)GridView1.Rows[i].FindControl("RbSample");
if (rbtn.Checked)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Radiocheck');", true);
}
}
}
这篇关于如何找到在GridView中的占位符内的动态控制(单选按钮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!