how to dynamically assign id to controls - controls are inside datalist in asp .net

1.1k views Asked by At

I am using datalist for showing datas. Datas are test questions with 4 options.I have 50 questions displaying in datalist.

By using ItemDataBound event - i am making visible only one question with 4 options to user. Remaining questions are invisible . In the Itemdatabound event - i am assigning id to the radio buttons. but also id is not assigned to the datalist ,it showing different 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();
}

In my design page code , using Datalist i am binding 50 questions . while binding itself i am hiding 2 to 50 questions . Question 1 only visible. I tried to give dynamic id for every questions and options using event databound and rowcreated. First it got error , after that i changed properties as ClientIDMode="Static" for controls.it shown different id. But if i click any button event again it clear the id's.again it shows old ids only.

0

There are 0 answers