1

我有一个带有 HTML Table 布局的转发器,如下所示,它从数据库中动态填充,

<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
    <table border="1">
        <tr>
            <th colspan="2"></th>
        </tr>
</HeaderTemplate>
<ItemTemplate>
    <tr>
        <td><%# DataBinder.Eval(Container.DataItem, "QuestionId") %> <asp:HiddenField ID="hiddenField" Value='<%# DataBinder.Eval(Container.DataItem, "QuestionId") %>' runat="server" /> </td>
        <td>
            <table>
                <tr>
                    <td colspan="4"><%# DataBinder.Eval(Container.DataItem, "Question") %> </td>
                </tr>
                <tr><td><asp:RadioButton ID="op1" runat="server" OnCheckedChange="check_Answer"/></td><td><%# DataBinder.Eval(Container.DataItem, "Answer1") %> </td></tr>
                <tr><td><asp:RadioButton ID="op2" runat="server" OnCheckedChange="check_Answer"/></td><td><%# DataBinder.Eval(Container.DataItem, "Answer2") %> </td></tr>
                <tr><td><asp:RadioButton ID="op3" runat="server" OnCheckedChange="check_Answer"/></td><td><%# DataBinder.Eval(Container.DataItem, "Answer3") %> </td></tr>
                <tr><td><asp:RadioButton ID="op4" runat="server" OnCheckedChange="check_Answer"/></td><td><%# DataBinder.Eval(Container.DataItem, "Answer4") %> </td></tr>
            </table>
        </td>
    </tr>
</ItemTemplate>
<FooterTemplate>
    </table>
</FooterTemplate>

在代码后面

protected void check_Answer(object sender, EventArgs e)
    {
        RepeaterItem item = (sender as RadioButton).Parent as RepeaterItem;
        HiddenField hiddenField = item.FindControl("hiddenField") as HiddenField;

        String questionId = hiddenField.Value;
        Session["test"] = questionId;/*Just added to a session and passed to test page*/
        Response.Redirect("test.aspx");/*This Page Displays the value of Session["test"]*/
    }

现在我需要做的是单选按钮的 OnCheckedChange 事件,它调用一个代码隐藏方法,但应该将 QuestionID 值传递给该方法。请建议我一种方法来检索包含“QuestionID”的值。有关更多信息,真实界面如下所示, 在此处输入图像描述

4

2 回答 2

3

您可以对选项按钮进行分组,并使用 questionid 作为组名的一部分。在代码中,您可以从 GroupName 获取 questionid。所以你真的不需要任何隐藏字段。您的标记可能如下所示:

<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <table border="1">
            <tr>
                <th colspan="2"></th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><%# DataBinder.Eval(Container.DataItem, "QuestionId") %> </td>
            <td>
                <table>
                    <tr>
                        <td colspan="4"><%# DataBinder.Eval(Container.DataItem, "Question") %> </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer1") %>' 
                                OnCheckedChanged="check_Answer" GroupName='<%# Eval("QuestionId","Grp_{0}") %>'  />                       
                        </td>
                        <td><asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer2") %>'  
                            OnCheckedChanged="check_Answer" GroupName='<%# Eval("QuestionId","Grp_{0}") %>'  />                        
                        </td>
                        <td><asp:RadioButton ID="RadioButton3" runat="server" AutoPostBack="true" Text='<%# DataBinder.Eval(Container.DataItem, "Answer3") %>'  
                            OnCheckedChanged="check_Answer" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' />
                        </td>
                        <td><asp:RadioButton ID="RadioButton4" runat="server" AutoPostBack="true"  Text='<%# DataBinder.Eval(Container.DataItem, "Answer4") %>'  
                            OnCheckedChanged="check_Answer" GroupName='<%# Eval("QuestionId","Grp_{0}") %>' />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

在代码中:

protected void check_Answer(object sender, EventArgs e)
{
    string grpId = ((RadioButton)sender).GroupName;
    string  questionId = grpId.Split('_')[1].ToString();

    Session["test"] = questionId;/*Just added to a session and passed to test page*/
    Response.Redirect("test.aspx");/*This Page Displays the value of Session["test"]*/
}
于 2013-09-15T16:04:13.527 回答
1

我将添加隐藏字段如下

<asp:HiddenField ID="hiddenField" Value='<%# DataBinder.Eval(Container.DataItem, "QuestionId") %>' runat="server" />

在你的check_Answer活动中

RepeaterItem item = (sender as RadioButton).Parent as RepeaterItem;
HiddenField hiddenField = item.FindControl("hiddenField") as HiddenField;

string questionId= hiddenField.Value;
于 2013-09-15T13:46:55.667 回答