[英]How to get values of CheckBoxes inside a gridview that are checked using asp .net
我在gridview中使用了復選框....我在第一個單元格中使用它....當我在運行時選擇復選框時,我需要獲取這些值...但是在選擇或單擊復選框時,它不是發現或價值是假的...如何在asp.net后端和c#代碼中寫?
<asp:TemplateField>
<ItemTemplate >
<asp:checkbox id="ShowAddress" runat="server" />
</ItemTemplate>
</asp:TemplateField>
代碼隱藏:
protected void Button1_Click(object sender, EventArgs e)
{
// Looping through all the rows in the GridView
foreach (GridViewRow di in GridView1.Rows)
{
CheckBox chkBx = (CheckBox)di.FindControl("ShowAddress");
if (chkBx != null && chkBx.Checked)
{
/// put your code here
}
}
}
頁面加載時腳本中是否有任何實現?
有人可以幫忙嗎?
StringCollection idCollection = new StringCollection();
string strID = string.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkDelete = (CheckBox) GridView1.Rows.Cells[0].FindControl("chkSelect");
if (chkDelete != null)
{
if (chkDelete.Checked)
{
strID = GridView1.Rows.Cells[1].Text;
idCollection.Add(strID);
}
}
}
有關更多詳細信息,請訪問以下鏈接: http : //www.itworld2.com/ghowto.aspx?id = 69
你如何填充你的GridView? 如果您在Page_Load中執行此操作,請確保您沒有在回發中執行此操作(請檢查IsPostBack)。
你的chkBx變量是null嗎?
以下代碼有效:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = row.Cells[0].Controls[0] as CheckBox;
if (chk != null && chk.Checked)
{
// ...
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Loadgridview();// its a correct
}// not Loadgridview() here if you load above error is occur
}
核實
int i = 0;
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = (CheckBox)GridView_AdminTags.Rows[i].Cells[0].FindControl("chkTag");
if (chk != null)
if (chk.Checked)
{
////.......;
}
i++;
}
i = 0;
如果使用以下行,Jakob Answer將起作用。 即使只有一個控件在單元格中,索引也需要為1而不是0
CheckBox chk = row.Cells[0].Controls[1] as CheckBox;
謝謝山姆
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.