如何动态地为控件分配id - 控件在asp .net中的datalist中

时间:2014-01-10 13:35:39

标签: asp.net c#-4.0 datalist itemdatabound

我正在使用datalist来显示数据。数据是4个选项的测试题。我有50个问题在datalist中显示。

通过使用ItemDataBound事件 - 我只向用户显示一个带有4个选项的问题。剩下的问题是看不见的。在Itemdatabound事件中 - 我正在为单选按钮分配id。但是id也没有分配给datalist,它显示了不同的id。

  <div class="widget">
                <div class="widget-header" align="center" style="background: #4a98c9; color: #FFFFFF;">
                    <h4>
                        <asp:Label ID="Label5" runat="server" Text="Question : "></asp:Label>
                        <asp:Label ID="lblQuestionNo" runat="server" Text="0 of 0"></asp:Label></h4>
                    <asp:HiddenField ID="HdCurrentQuestSlNo" runat="server" />
                    <asp:HiddenField ID="HdfMinTime" runat="server" />
                    <asp:HiddenField ID="HdfSecTime" runat="server" />
                    <asp:HiddenField ID="HiddenField1" runat="server" />
                    <asp:HiddenField ID="HdfQSlNo" runat="server" />
                    <asp:HiddenField ID="HdQuestCount" runat="server" Value="0"/>
                </div>
                <div class="widget-content">
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                        <ContentTemplate>
                            <asp:Panel ID="PanelQuestBlock" runat="server" ScrollBars="Vertical" Width="100%" Height="325px">
                                <asp:DataList runat="server" ID="DataListQuestion" 
                                    onitemdatabound="DataListQuestion_ItemDataBound" 
                                    onitemcreated="DataListQuestion_ItemCreated">
                                    <ItemTemplate>
                                        <div class="questdisplayId" id="divQuestBlock" runat="server">
                                            <div>
                                                <asp:HiddenField ID='HdSlNo' runat="server" Value='<%# Eval("SlNo") %>' ClientIDMode="Static"/>
                                                <asp:HiddenField ID="HdUserQuestId" runat="server" Value='<%# Eval("UserQuestionId") %>' ClientIDMode="Static"/>
                                                <asp:HiddenField ID="HdTotalOptions" runat="server" Value='<%# Eval("TotalOptions") %>' ClientIDMode="Static"/>
                                                <asp:HiddenField ID="HdQuestAnsweredTimeSecs" runat="server" Value='<%# Eval("AnsweredTimeSecs") %>' ClientIDMode="Static"/>
                                                <asp:HiddenField ID="HdOptionAnswered" runat="server" Value='<%# Eval("OptionAnswered") %>' ClientIDMode="Static"/>
                                            </div>
                                            <div>
                                                <asp:Label ID="Question" runat="server" Text='<%# Eval("Question") %>'></asp:Label>
                                            </div>
                                            <div class="answer-row" id="divOption1" runat="server">
                                                <asp:RadioButton ID="Option1" runat="server" GroupName="Answer" value="1" Text='<%# Eval("Option1") %>'
                                                    CssClass="rdbtn" Style="vertical-align: middle" ClientIDMode="Static"/>
                                                <asp:Label ID="lblOpt1" runat="server" Text=""></asp:Label>
                                            </div>
                                            <div class="answer-row" id="divOption2" runat="server">
                                                <asp:RadioButton ID="Option2" runat="server" GroupName="Answer" value="2" Text='<%# Eval("Option2") %>'
                                                    CssClass="rdbtn" Style="vertical-align: middle" ClientIDMode="Static"/>
                                                <asp:Label ID="lblOpt2" runat="server" Text=""></asp:Label>
                                            </div>
                                            <div class="answer-row" id="divOption3" runat="server">
                                                <asp:RadioButton ID="Option3" runat="server" GroupName="Answer" value="3" Text='<%# Eval("Option3") %>'
                                                    CssClass="rdbtn" Style="vertical-align: top; left: auto;" ClientIDMode="Static"/>
                                                <asp:Label ID="lblOpt3" runat="server" Text=""></asp:Label>
                                            </div>
                                            <div class="answer-row" id="divOption4" runat="server">
                                                <asp:RadioButton ID="Option4" runat="server" GroupName="Answer" value="4" Text='<%# Eval("Option4") %>'
                                                    CssClass="rdbtn" Style="vertical-align: text-top" ClientIDMode="Static"/>
                                                <asp:Label ID="lblOpt4" runat="server" Text=""></asp:Label>
                                            </div>  
                                            <div class="answer-row" id="divOption5" runat="server">
                                                <asp:RadioButton ID="Option5" runat="server" GroupName="Answer" value="4" Text='<%# Eval("Option5") %>'
                                                    CssClass="rdbtn" Style="vertical-align: text-top" ClientIDMode="Static"/>
                                                <asp:Label ID="lblOpt5" runat="server" Text=""></asp:Label>
                                            </div>                                               
                                        </div>
                                    </ItemTemplate>
                                </asp:DataList>
                            </asp:Panel>
                            <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                                <ContentTemplate>
                                    <div class="row">
                                        <div class="span8" align="center">                                                
                                            <asp:Button ID="btnClear" runat="server" Text="Clear Selection" 
                                                CssClass="btn btn-warning" OnClientClick="Clearbutton();" 
                                                />
                                            <asp:Button ID="btnSave" runat="server" Text="Save & Next" CssClass="btn btn-success" OnClientClick="Savebutton()"/>
                                        </div>
                                        <div class="span4" align="center">
                                        <asp:Button ID="btnReview" runat="server" Text="Mark Review & Next" CssClass="btn btn-info" OnClientClick="Reviewbutton()"/>
                                            <asp:Button ID="btnFinish" runat="server" Text="Finish" CssClass="btn btn-danger" OnClientClick="Finishbutton()"/>
                                        </div>
                                    </div>
                                </ContentTemplate>
                                <Triggers>
                                    <asp:AsyncPostBackTrigger ControlID="btnReview" />
                                    <asp:AsyncPostBackTrigger ControlID="btnClear" />
                                    <asp:AsyncPostBackTrigger ControlID="btnSave" />
                                </Triggers>
                            </asp:UpdatePanel>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="DataListQuestion" />
                        </Triggers>
                    </asp:UpdatePanel>
                </div>
            </div>

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {                
HiddenField HdSlNo = (HiddenField)e.Item.FindControl("HdSlNo");
                RadioButton Option1 = (RadioButton)e.Item.FindControl("Option1");
                RadioButton Option2 = (RadioButton)e.Item.FindControl("Option2");
                RadioButton Option3 = (RadioButton)e.Item.FindControl("Option3");
                RadioButton Option4 = (RadioButton)e.Item.FindControl("Option4");
                RadioButton Option5 = (RadioButton)e.Item.FindControl("Option5");

                Option1.ID = "rdbtnoption1_" + HdSlNo.Value.ToString();
                Option2.ID = "rdbtnoption2_" + HdSlNo.Value.ToString();
                Option3.ID = "rdbtnoption3_" + HdSlNo.Value.ToString();
                Option4.ID = "rdbtnoption4_" + HdSlNo.Value.ToString();
                Option5.ID = "rdbtnoption5_" + HdSlNo.Value.ToString();
}

在我的设计页面代码中,使用Datalist我绑定了50个问题。绑定自己我隐藏了2到50个问题。问题1仅可见。我尝试使用事件数据绑定和rowcreated为每个问题和选项提供动态ID。首先它得到了错误,之后我将属性更改为ClientIDMode =“Static”for controls.it显示了不同的id。但是,如果我再次单击任何按钮事件,则清除id's。仅显示旧ID。

0 个答案:

没有答案