链接到单选按钮值,数据未绑定,asp.net c#
嗨,我需要稍微修改一下我的代码。我有一个带有单选按钮列表和文本区域的页面。当用户进行单选按钮选择时,文本区域将被填充。
此外,当用户进行单选按钮选择时,URL 中将包含一个扩展名,以显示他们选择了哪个选择索引号。 (即?selected=0)
http://test.com/frm_Articles.aspx?selected=0< /a> http://test.com/frm_Articles.aspx?selected=1 http://test.com/frm_Articles.aspx?selected=2
这样他们就可以复制该网址并在其他网站中将其作为链接引用。或将其放入他们的收藏夹中。
问题是,如果您获取 URL 并打开新浏览器,页面不会相应地传递值和数据绑定。页面上没有显示单选按钮或内容。 我认为一定是回发逻辑???
工作原理:
- 当我启动网站时,单选按钮出现,并且索引 0 被设置,
- 当我选择单选按钮时,正确的数据显示,并且链接到单选按钮值的 URL 显示在浏览器中(即 http://test.com/test.aspx?selected=2)
- 如果我在同一浏览器中剪切并粘贴指针网址,则正确的数据是呈现出
不起作用的内容(所有处理错误回发的内容):
1.当我启动网站时,即使单选按钮设置为 0 索引并且可见,文本区域内也不会出现任何数据。 2. 如果我将指针 URL 剪切并粘贴到新浏览器中,则不会显示文本区域和单选按钮。
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
int selected;
if (Request.QueryString["selected"] != null)
{
if (int.TryParse(Request.QueryString["selected"], out selected))
{
RadioButtonList1.SelectedIndex = selected;
RadioButtonList1.DataBind();
}
}
else
{
int firstart = 0;
RadioButtonList1.SelectedIndex = firstart;
RadioButtonList1.DataBind();
}
}
}
protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
//
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
try{
e.Command.Parameters["@URL_FK"].Value = Session["URL_PK"];
}
catch (Exception ex)
{
}
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strRedirect;
strRedirect = "test.aspx?selected=" + RadioButtonList1.SelectedIndex;
Response.Redirect(strRedirect);
}
}
hi i need to modify my code a little bit. i have a page with a radio button list and a textarea. the textarea is populated when a users makes a radio button selection.
also, when a user makes a radio button selection the url will hold an extention in the url to show which selection index number they have selection. (i.e. ?selected=0)
http://test.com/frm_Articles.aspx?selected=0
http://test.com/frm_Articles.aspx?selected=1
http://test.com/frm_Articles.aspx?selected=2
that way they can copy the url and reference it in other websites as a link. or place it in their favorites.
the problem is, if you grab the url and open a new browser, the page does not pass the value and databind accordingly. no radio buttons or content appear on the page.
must be the postback logic i think???
what's working:
- when i launch the website the radio buttons appear and index 0 is set
- when i select radio buttons the correct data displays and urls linking to radio button values display in browser (i.e. http://test.com/test.aspx?selected=2)
- if i cut and paste pointer urls within the same browser then correct data is rendered
what doesn't work (everything that deal with an false PostBack):
1.when i launch website no data within the textarea apprears even though the radio button is set to 0 index and is visiable.
2. if i cut and paste pointer url into a new browser, text area and radio buttons do not display.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
int selected;
if (Request.QueryString["selected"] != null)
{
if (int.TryParse(Request.QueryString["selected"], out selected))
{
RadioButtonList1.SelectedIndex = selected;
RadioButtonList1.DataBind();
}
}
else
{
int firstart = 0;
RadioButtonList1.SelectedIndex = firstart;
RadioButtonList1.DataBind();
}
}
}
protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
//
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
try{
e.Command.Parameters["@URL_FK"].Value = Session["URL_PK"];
}
catch (Exception ex)
{
}
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strRedirect;
strRedirect = "test.aspx?selected=" + RadioButtonList1.SelectedIndex;
Response.Redirect(strRedirect);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
之前的
Page_Load
事件中,在您的代码中,此行
RadioButtonList1.SelectedIndex = selected;
您应该绑定 RadioButtonList1。绑定RadioButtonList后,您可以设置
SelectedIndex
。In your code at
Page_Load
event before this lineRadioButtonList1.SelectedIndex = selected;
you should bind RadioButtonList1. after binding RadioButtonList you can set
SelectedIndex
.我的 SqlDataSource1_Selecting 方法是问题所在。我使用了另一种方法并且我的代码有效。
my SqlDataSource1_Selecting method was the issue. i used another approach and my code worked.