asp.net,radiobuttonlist,在url中显示记录数
我有一个单选按钮列表,在选择时查询表格并在文本区域中显示数据。现在我正在尝试增强我的代码,以便单选按钮的值显示在 URL 中。这样我就可以向用户发送指向无线电选择显示的内容(即文章)的链接。
到目前为止,当用户做出选择时,我已经能够成功地将“Article_PK”放入网址中。现在我只是专注于如何让我的数据绑定再次工作。所以基本上,我需要读取 url 中“Article_PK”的值以在文本区域中显示数据。需要一个具有疯狂编码技能的人来拯救世界!
后面的代码 --------
public partial class frm_Articles : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
RadioButtonList1.SelectedIndex = 0;
RadioButtonList1.DataBind();
}
else
{
string strRedirect;
strRedirect = "frm_Articles.aspx?Article_PK=" + RadioButtonList1.SelectedValue.ToString();
Response.Redirect(strRedirect);
}
}
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)
{
}
}
}
----- aspx
<tr valign="top">
<td width="75%">
<!-- Body -->
<asp:DetailsView ID="DetailsView3" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource2" Height="100%" Width="100%" GridLines="None">
<Fields>
<asp:TemplateField HeaderText="ArticleText" ShowHeader="False" SortExpression="ArticleText">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ArticleText") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="*"></asp:RequiredFieldValidator>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ArticleText") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="*"></asp:RequiredFieldValidator>
</InsertItemTemplate>
<ItemTemplate>
<%# Eval("ArticleText") %>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:CSFConnectionString %>" SelectCommand="SELECT [ArticleText], [Title] FROM [TEST_Article] WHERE ([Article_PK] = @Article_PK)" onselecting="SqlDataSource2_Selecting">
<SelectParameters>
<asp:ControlParameter ControlID="RadioButtonList1" DefaultValue="1" Name="Article_PK" PropertyName="SelectedValue" Type="Int32" ConvertEmptyStringToNull="True" />
</SelectParameters>
</asp:SqlDataSource>
</td>
<td width="25%" align="left" valign="top">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TESTConnectionString %>" SelectCommand="SELECT Article_PK, ArticleText, Score, Title, Url_FK FROM TEST_Article WHERE (Url_FK = @URL_FK)" onselecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:Parameter DefaultValue="0" Name="Url_FK" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<h2>Articles</h2>
<h2>Select an article below:</h2>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Title" DataValueField="Article_PK">
</asp:RadioButtonList>
<br />
</td></tr>
i have a radio button list that on selecting, queries a table and displays data in a textarea. now i'm trying to enhance my code so that the value of the radio button is displayed within the URL. that way i can send users links to content displayed by the radio selection (i.e. articles).
So far i've been able to place the "Article_PK" within the url successfully when the user makes a selection. now i'm just stuck on how to get my databind working again. so basically, i need to read in the value of the "Article_PK" in the url to display the data within the textarea. need someone with some mad coding skills to save the day!
code behind --------
public partial class frm_Articles : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
RadioButtonList1.SelectedIndex = 0;
RadioButtonList1.DataBind();
}
else
{
string strRedirect;
strRedirect = "frm_Articles.aspx?Article_PK=" + RadioButtonList1.SelectedValue.ToString();
Response.Redirect(strRedirect);
}
}
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)
{
}
}
}
----- aspx
<tr valign="top">
<td width="75%">
<!-- Body -->
<asp:DetailsView ID="DetailsView3" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource2" Height="100%" Width="100%" GridLines="None">
<Fields>
<asp:TemplateField HeaderText="ArticleText" ShowHeader="False" SortExpression="ArticleText">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ArticleText") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="*"></asp:RequiredFieldValidator>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ArticleText") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="*"></asp:RequiredFieldValidator>
</InsertItemTemplate>
<ItemTemplate>
<%# Eval("ArticleText") %>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:CSFConnectionString %>" SelectCommand="SELECT [ArticleText], [Title] FROM [TEST_Article] WHERE ([Article_PK] = @Article_PK)" onselecting="SqlDataSource2_Selecting">
<SelectParameters>
<asp:ControlParameter ControlID="RadioButtonList1" DefaultValue="1" Name="Article_PK" PropertyName="SelectedValue" Type="Int32" ConvertEmptyStringToNull="True" />
</SelectParameters>
</asp:SqlDataSource>
</td>
<td width="25%" align="left" valign="top">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TESTConnectionString %>" SelectCommand="SELECT Article_PK, ArticleText, Score, Title, Url_FK FROM TEST_Article WHERE (Url_FK = @URL_FK)" onselecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:Parameter DefaultValue="0" Name="Url_FK" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<h2>Articles</h2>
<h2>Select an article below:</h2>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Title" DataValueField="Article_PK">
</asp:RadioButtonList>
<br />
</td></tr>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您没有在
PageLoad
事件中构建任何内容来处理查询字符串,那么当控件执行回发时,什么也不会发生。在 pageLoad 事件中,您需要类似的内容:
将单选按钮设置为 autopostback="true" 并且您应该获得所需的效果。
If you didn't build anything in your
PageLoad
event to handle the query string, then nothing will happen when the controls does a postback.Inside your pageLoad event you want something like:
Set your radio buttons to have
autopostback="true"
and you should get the desired effect.