1

I developed an user control and added that user control in my visual web part multiple times.

Three times manuallay and then dynamically. I can fetch the data of the user control which i have added manually But I am unable to fetch the data of the user control that I have added dynamically. Please suggest.

Codes of the visual web part are as follows

Visual web part ascx page

// my user control registration in web part
<%@ Register TagPrefix="UserControlsTkt" TagName="MultiSubTkts" Src="../ucMutlSubTicket.ascx" %>
<%@ Reference Control="../ucMutlSubTicket.ascx" %>

<asp:UpdatePanel ID="updatpnl" UpdateMode="Conditional" runat="server">
            <ContentTemplate>
                <asp:Panel ID="dynamicpnl" runat="server">
                  <table width="100%" cellpadding="0" cellspacing="0" class="yellow-headds CSSTableGenerator contentSL2" runat="server" id="tblDynmcContorls">
                         <tr class="small-box">
                            <td style="background: #f1f1f1;">
                            <UserControlsTkt:MultiSubTkts id="mutlisubtktrow0" runat="server"></UserControlsTkt:MultiSubTkts>
                                </td>
                        </tr>
                        <tr class="small-box">
                             <td style="background: #f1f1f1;">
                            <UserControlsTkt:MultiSubTkts id="mutlisubtktrow1" runat="server"></UserControlsTkt:MultiSubTkts>
                            </td>
                        </tr>
                        <tr class="small-box">
                           <td style="background: #f1f1f1;">
                                <UserControlsTkt:MultiSubTkts id="mutlisubtktrow2" runat="server" ></UserControlsTkt:MultiSubTkts>
                            </td>
                        </tr>
                      <tr>
                          <td>
                                <asp:ImageButton ID="addnewSctn" OnClick="addnewSctn_Click" runat="server" ImageUrl="../../../../Style Library/SCMSModule/images/plus_16.png" class="add-txt-sub-category" />
                                <asp:ImageButton ID="removeNewSctn"  OnClick="removeNewSctn_Click" runat="server" ImageUrl="../../../../Style Library/SCMSModule/images/minus_16.png" />

                            </td>
                      </tr>
                      <tr class="small-box">
                          <td>
                            <%--<asp:PlaceHolder ID="plcholdr" runat="server">

                            </asp:PlaceHolder>--%>

                              </td>
                      </tr>
                        <tr class="tr-btn-event">
                            <td valign="middle" align="center" colspan="2" class="tdStl popupmargin">
                                <asp:Button ID="btnAddMultSubTkt" runat="server" Text="Create" CssClass="btn" OnClick="btnAddMultSubTkt_Click" />
                            </td>
                        </tr>
                    </table>
                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>

The code behind file

protected void Page_Init(object sender, EventArgs e)
        {
            if (ViewState["iValue"] != null)
            {
                int i = Convert.ToInt32(ViewState["iValue"]);
                for (int j = 3; j < i; j++)
                {
                    ucMutlSubTicket ac = (ucMutlSubTicket)LoadControl("../ucMutlSubTicket.ascx");
                    ac.ID = "mycontrols" + j;
                    ac.Attributes.Add("runat", "server");
                    //plcholdr2.Controls.Add(ac);
                    //plcholdr2.Controls.Add(new LiteralControl("<BR />"));
                    HtmlTableRow tRow = new HtmlTableRow();
                    HtmlTableCell tb = new HtmlTableCell();
                    tb.Style.Add("background", "#f1f1f1");
                    tb.Controls.Add(ac);
                    tRow.Attributes.Add("class", "small-box");
                    tRow.Controls.Add(tb);
                    tblDynmcContorls.Rows.Add(tRow);
                }
            }
        }

 protected void btnAddMultSubTkt_Click(object sender, EventArgs e)
        {

            ucMutlSubTicket objtstCntrl = (ucMutlSubTicket)dynamicpnl.FindControl("mycontrols3"); // here i dont  get the control
            ucMutlSubTicket objtstCntrl1 = (ucMutlSubTicket)dynamicpnl.FindControl("mutlisubtktrow0"); // here I get the control
            TextBox objTextBox = (TextBox)objtstCntrl.FindControl("txtSubjct");
            string strFirstName = objTextBox.Text;
        }

protected void addnewSctn_Click(object sender, ImageClickEventArgs e)
        {
            int i = 3;
            if(ViewState["iValue"]!=null)
            {
                i = Convert.ToInt32(ViewState["iValue"]);
            }
            if (Convert.ToInt32(ViewState["iValue"]) < 3)
            {
                i = 3;
            }
            i += 1;
            ViewState["iValue"] = i;
            this.Page_Init(null, null);
            //this.Page_Load(null, null);
        }

        protected void removeNewSctn_Click(object sender, ImageClickEventArgs e)
        {
            int i=0;
            if (ViewState["iValue"] != null)
            {
                i = Convert.ToInt32(ViewState["iValue"]);
                i -= 1;
                ViewState["iValue"] = i;
                this.Page_Init(null, null);
                //this.Page_Load(null, null);
            }
            if (Convert.ToInt32(ViewState["iValue"]) < 3)
            {
                i = 3;
            }
        }

Please help.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.