asp.net,radiobuttonlist,在url中显示记录数

发布于 2024-10-12 07:32:13 字数 4672 浏览 6 评论 0原文

我有一个单选按钮列表,在选择时查询表格并在文本区域中显示数据。现在我正在尝试增强我的代码,以便单选按钮的值显示在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞觅青森が 2024-10-19 07:32:14

如果您没有在 PageLoad 事件中构建任何内容来处理查询字符串,那么当控件执行回发时,什么也不会发生。

在 pageLoad 事件中,您需要类似的内容:

if(IsPostBack)
{
    if(Request.QueryString["URL_PK"] != null)
    {
    int url_pk;
    if(int.TryParse(Request.QueryString["URL_PK"].ToString(), out url_pk)
    {
         //do whatever you need to do with url pk here
    }
}

将单选按钮设置为 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:

if(IsPostBack)
{
    if(Request.QueryString["URL_PK"] != null)
    {
    int url_pk;
    if(int.TryParse(Request.QueryString["URL_PK"].ToString(), out url_pk)
    {
         //do whatever you need to do with url pk here
    }
}

Set your radio buttons to have autopostback="true" and you should get the desired effect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文