![](/img/trans.png)
[英]How to get selected radio button in selected list item from wpf listbox
[英]How to get cell value from gridview when radio button list item is selected?
我有一个gridview从SQL表动态显示数据。 这部分没问题。 我添加了一个包含单选按钮列表的库。 如果用户在单击“提交”按钮时选择了这些行的单选按钮列表项,那么我想获取第3列(索引2)的所有单元格值。
我的GridView:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
RadioButtonList
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButtonList ID="Radio1" runat="server">
<asp:ListItem Value="1" Text="OK" />
<asp:ListItem Value="0" Text="KO" />
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Submit" runat="server" Text="Submit" OnClick="Submit_Click"/>
protected void Page_Load(object sender, EventArgs e)
{
string strSQLconnection = "Connection to DB";
SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
SqlCommand sqlCommand = new SqlCommand("SELECT Champ1, Champ2, Camp3 FROM Table1 WHERE Condition1 IS NULL AND Condition2 IS NULL", sqlConnection);
sqlConnection.Open();
SqlDataReader reader = sqlCommand.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
sqlConnection.Close();
}
编辑:Gridview示例
---------------------------------------------
Radio | Column0 | Column1 | Column2 |
---------------------------------------------
°OK °KO | abc | abc | abc |
---------------------------------------------
°OK °KO | abc | abc | abc |
---------------------------------------------
°OK °KO | abc | abc | abc |
---------------------------------------------
°OK °KO | abc | abc | abc |
---------------------------------------------
°OK °KO | abc | abc | abc |
---------------------------------------------
°OK °KO | abc | abc | abc |
--------------------------------------------
如果选择了单选列表项,则在单击“提交”按钮时,我想获取column1中相应单元格的值。
我的代码背后:
protected void Submit_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
//Find the Radio button control
RadioButtonList rb = (RadioButtonList)row.FindControl("Radio1");
if (rb.SelectedItem.Text == "OK")
{
string id = row.Cells[2].Text;
string query = "Query";
SqlConnection con = new SqlConnection("Connection to DB");
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
added = cmd.ExecuteNonQuery();
con.Close();
}
}
}
提交时遇到的错误:
[NullReferenceException: Object reference not set to an instance of an object.]
MasterPage.Submit_Click(Object sender, EventArgs e) +202
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +114
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +139
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +28
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2980
不知道该如何处理。
扩展“ Selva TS”给出的答案(和我的评论)
您有两个问题,正如Selva指出的那样,在使用RadioButtonList Selected项之前,应先验证其是否为null。 这样做将停止您的错误,但仍不能解决问题,存在的问题是Page_Load正在触发并重新绑定网格视图,从而具有重置单选按钮列表的作用。
要解决此问题,请将您的Page_Load更改为
protected void Page_Load(object sender, EventArgs e)
{
if (!isPostBack)
{
string strSQLconnection = "Connection to DB";
SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
SqlCommand sqlCommand = new SqlCommand("Query", sqlConnection);
sqlConnection.Open();
SqlDataReader reader = sqlCommand.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
sqlConnection.Close();
}
}
仅当第一次在页面上加载(或刷新)时,这才会绑定gridview,而如果其中一个控件引起回发,则不会绑定。
然后,您需要修改提交点击,如Selva所示
protected void Submit_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
//Find the Radio button control
RadioButtonList rb = (RadioButtonList)row.FindControl("Radio1");
if (rb.SelectedItem != null)
{
if (rb.SelectedItem.Text == "OK")
{
string id = row.Cells[2].Text;
string query = "Query";
SqlConnection con = new SqlConnection("Connection to DB");
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
added = cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
只需更改page_load即可解决问题本身,但实际上您应该检查SelectedItem是否为null,以防万一! -用户可能是傻瓜,您应该始终尝试预见他们的愚蠢!
问题是,如果未选择任何RadioButton
,则SelectedItem
始终为null
。 只需在RadioButton
控件中添加一个验证,无论SelectedItem
是否为null
。
在验证SelectedItem
之前在代码中添加if (rb.SelectedItem != null)
条件会起作用,
protected void Submit_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
//Find the Radio button control
RadioButtonList rb = (RadioButtonList)row.FindControl("Radio1");
if (rb.SelectedItem != null)
{
if (rb.SelectedItem.Text == "OK")
{
string id = row.Cells[2].Text;
string query = "Query";
SqlConnection con = new SqlConnection("Connection to DB");
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
added = cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
另外,如d3vy建议的那样,在Page_Load
添加if (!Page.IsPostBack)
将停止擦除RadioButtonList
的SelectedItem
。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string strSQLconnection = "Connection to DB";
SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
SqlCommand sqlCommand = new SqlCommand("Query", sqlConnection);
sqlConnection.Open();
SqlDataReader reader = sqlCommand.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
sqlConnection.Close();
}
}
几乎有一个类似的问题,使用输入代替了asp:radiobutton
:
<asp:TemplateField HeaderText="Select One">
<ItemTemplate>
<input name="MyRadioButton" type="radio"
value='<%# Eval("CategoryID") %>' />
</ItemTemplate>
</asp:TemplateField>
然后在buttonClick
使用request.form获取选定radiobutton
的值。 如果未选择,它将返回null。
protected void Button1_Click(object sender, EventArgs e)
{
string selectedValue = Request.Form["MyRadioButton"];
lblMsg.Text = selectedValue;
}
这是更多信息的链接: http : //www.codeproject.com/Articles/13050/RadioButtons-inside-a-GridView-control
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.