单选按钮列表选择的值在asp.net中不是持久的 [英] Radio button list selected value not persistent in asp.net
问题描述
我有两个动态填充的单选按钮列表。第一个按钮单击时填充第一个,第一个单选按钮列表的更改事件中的另一个填充。问题只出在第二个列表中。问题是我无法在InsertButton_Click方法中检索第二个单选按钮列表的更改值(标记为**)。它总是返回默认索引值,即0.我在page_load事件中没有任何内容。我读了很多类似的问题,但似乎都没有帮助。请指导。下面是相同的asp和c#代码:
ASP:
< asp:按钮 id = SaveButton
< span class =code-attribute> 文本 = 保存
runat = server onclick = SaveButton_Click >
< / asp:按钮 >
< asp:Button id = VisualiseButton
文字 = 可视化
runat = server onclick = VisualiseButton_Click >
< / asp :按钮 >
< asp:RadioButtonList id = RadioButtonList2 runat < span class =code-keyword> = ser ver 可见 = false
< span class =code-attribute> onselectedindexchanged = RadioButtonList2_SelectedIndexChanged > < / asp:RadioButtonList >
< asp: RadioButtonList id = RadioButtonList1 runat = server 可见 = false > < / asp:RadioButtonList >
< asp:按钮 id = SaveToDBButton
文字 = 插入
runat = server 可见 = false < span class =code-attribute> onclick = InsertButton_Click >
C#:
protected void SaveButton_Click( object sender,EventArgs e)
{
字符串 selQuery = SELECT id,name FR OM类别;
尝试
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(selQuery,con);
DataTable dt = new DataTable();
da.Fill(dt);
RadioButtonList2.DataSource = dt;
RadioButtonList2.DataTextField = name;
RadioButtonList2.DataValueField = id;
RadioButtonList2.DataBind();
RadioButtonList2.RepeatColumns = 3 ;
RadioButtonList2.AutoPostBack = true ;
RadioButtonList2.Visible = true ;
}
catch (SqlException ex)
{
}
最后
{
如果(con! = null )
{
con.Close();
}
}
}
受保护 void RadioButtonList2_SelectedIndexChanged( object sender,EventArgs e)
{
String catId = RadioButtonList2.SelectedValue;
SqlCommand cmdselect = new SqlCommand( SELECT DISTINCT categoryId,linkTablesCategories.tableName,tableDescription FROM linkTablesCategories,tableDescriptions其中linkTablesCategories.tableName = tableDescriptions.tableName和categoryId =' + catId + '跨度>);
RadioButtonList1.Items.Clear();
尝试
{
con.Open();
cmdselect.Connection = con;
SqlDataReader dar = cmdselect.ExecuteReader();
if (dar.HasRows)
{
while (dar .Read())
{
ListItem li = new ListItem(dar [ tableName]。ToString(),dar [ 。的categoryId跨度>]的ToString());
li.Attributes.Add( title,dar [ tableDescription]。ToString());
RadioButtonList1.Items.Add(li);
}
}
RadioButtonList1.Visible = true ;
SaveToDBButton.Visible = true ;
}
catch (SqlException ex)
{
// lblMessage.Text = ex.Message;
}
finally
{
cmdselect.Dispose();
if (con!= null )
{
con。关();
}
}
}
受保护 void InsertButton_Click( object sender,EventArgs e)
{
String tableId = ;
** tableId = RadioButtonList1.SelectedItem.Text; **
String path = Server.MapPath( 〜/);
string filepath = path + Session [ 。文件路径跨度>]的ToString();
StreamReader sr = new StreamReader(filepath);
string line = sr.ReadLine();
string [] value = line.Split(' ,');
DataTable dt = new DataTable();
DataRow行;
foreach (字符串 dc value )
{
dt.Columns.Add( new DataColumn(dc ));
}
while (!sr.EndOfStream)
{
value = sr.ReadLine()。分割(' ,');
if ( value .Length == dt.Columns.Count)
{
row = dt.NewRow();
row.ItemArray = value ;
dt.Rows.Add(row);
}
}
SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString,SqlBulkCopyOptions.TableLock);
bc.DestinationTableName = tableId;
bc.BatchSize = dt.Rows.Count;
con.Open();
bc.WriteToServer(dt);
bc.Close();
con.Close();
}
嘿,刚刚遇到问题,就是这行:
< pre lang =c#> ListItem li = new ListItem(dar [ tableName]。ToString(),dar [ categoryId] .ToString());
categoryId是一个常数值因此问题,再次是我的坏。
谢谢
I have two dynamically populated radio button lists. The first one gets populated on a button click, the other one on the change event of the first radio button list. The problem is only with the second list. The issue is that I am not able to retrieve the changed value of the second radio button list in the InsertButton_Click method(marked by **). It always returns the default index value i.e 0. I don''t have anything in page_load event. I read quite a few similar questions but none seem to help. Please guide. Below is the asp and c# code for the same:
ASP:
<asp:Button id="SaveButton"
Text="Save"
runat="server" onclick="SaveButton_Click">
</asp:Button>
<asp:Button id="VisualiseButton"
Text="Visualise"
runat="server" onclick="VisualiseButton_Click">
</asp:Button>
<asp:RadioButtonList id="RadioButtonList2" runat="server" Visible="false"
onselectedindexchanged="RadioButtonList2_SelectedIndexChanged" ></asp:RadioButtonList>
<asp:RadioButtonList id="RadioButtonList1" runat="server" Visible="false" ></asp:RadioButtonList>
<asp:Button id="SaveToDBButton"
Text="Insert"
runat="server" Visible="false" onclick="InsertButton_Click">
C#:
protected void SaveButton_Click(object sender, EventArgs e)
{
String selQuery = "SELECT id, name FROM categories";
try
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(selQuery, con);
DataTable dt = new DataTable();
da.Fill(dt);
RadioButtonList2.DataSource = dt;
RadioButtonList2.DataTextField = "name";
RadioButtonList2.DataValueField = "id";
RadioButtonList2.DataBind();
RadioButtonList2.RepeatColumns = 3;
RadioButtonList2.AutoPostBack = true;
RadioButtonList2.Visible = true;
}
catch (SqlException ex)
{
}
finally
{
if (con != null)
{
con.Close();
}
}
}
protected void RadioButtonList2_SelectedIndexChanged(object sender, EventArgs e)
{
String catId = RadioButtonList2.SelectedValue;
SqlCommand cmdselect = new SqlCommand("SELECT DISTINCT categoryId, linkTablesCategories.tableName, tableDescription FROM linkTablesCategories, tableDescriptions where linkTablesCategories.tableName = tableDescriptions.tableName and categoryId ='" + catId + "'");
RadioButtonList1.Items.Clear();
try
{
con.Open();
cmdselect.Connection = con;
SqlDataReader dar = cmdselect.ExecuteReader();
if (dar.HasRows)
{
while (dar.Read())
{
ListItem li = new ListItem(dar["tableName"].ToString(), dar["categoryId"].ToString());
li.Attributes.Add("title", dar["tableDescription"].ToString());
RadioButtonList1.Items.Add(li);
}
}
RadioButtonList1.Visible = true;
SaveToDBButton.Visible = true;
}
catch (SqlException ex)
{
//lblMessage.Text = ex.Message;
}
finally
{
cmdselect.Dispose();
if (con != null)
{
con.Close();
}
}
}
protected void InsertButton_Click(object sender, EventArgs e)
{
String tableId="";
**tableId = RadioButtonList1.SelectedItem.Text;**
String path = Server.MapPath("~/");
string filepath = path + Session["filepath"].ToString();
StreamReader sr = new StreamReader(filepath);
string line = sr.ReadLine();
string[] value = line.Split(',');
DataTable dt = new DataTable();
DataRow row;
foreach (string dc in value)
{
dt.Columns.Add(new DataColumn(dc));
}
while (!sr.EndOfStream)
{
value = sr.ReadLine().Split(',');
if (value.Length == dt.Columns.Count)
{
row = dt.NewRow();
row.ItemArray = value;
dt.Rows.Add(row);
}
}
SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
bc.DestinationTableName = tableId;
bc.BatchSize = dt.Rows.Count;
con.Open();
bc.WriteToServer(dt);
bc.Close();
con.Close();
}
Hey just got the issue, it was with the line:
ListItem li = new ListItem(dar["tableName"].ToString(), dar["categoryId"].ToString());
The categoryId was a constant value and thus the issue, again my bad.
Thanks
这篇关于单选按钮列表选择的值在asp.net中不是持久的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!