Hello and Greetings.
Sorry if this appears to be a bit basic in nature, but here's my problem. I have a tabcontainer and in one of the panels I got a gridview. When the page loads first time the grid show show a label with text 'SessioID' and in the cell beside it a blank textbox. Now in the footer template I got a button which is there for adding rows at runtime basically such that the label in each new row has text 'Parameter 2', 'Parameter 3' and so on as well as the blank textbox in the adjacent cell. Pretty straightforward as I have done similar things in simple web forms. But my code just not working inside the tab container. Maybe there is some issue of post back or rather the non occurence of it, or some other logical error in my code. I am posting my code for you to have a look.
The mark up---><ajaxToolkit:TabPanel ID="tbpParameters" runat="server" HeaderText="AffiliateParameters"><HeaderTemplate>Affiliate Parameters</HeaderTemplate><ContentTemplate><div id="anchor2" class="anchor tabContHld" style="width: auto!important;"><h3 class="formHeading">
Affiliate Parameters Registration</h3><asp:Panel ID="pnlParameters" runat="server"><asp:GridView ID="gvParameters" ShowFooter="True" runat="server"
AutoGenerateColumns="False"><Columns><asp:BoundField DataField="SerialNumber" HeaderText="Serial Number" /><asp:TemplateField HeaderText="Parameter Name"><ItemTemplate><asp:Label ID="lbl1" runat="server" Text='<%# Eval("Column1") %>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Pattern"><ItemTemplate><asp:TextBox CssClass="fromborder" ID="txtParam" runat="server"></asp:TextBox></ItemTemplate><FooterStyle HorizontalAlign="Center" /><FooterTemplate><asp:Button ID="btnAddRows" runat="server" CssClass="loginbtn" Text="ADD MORE" OnClick="btnAddRow_Click" /></FooterTemplate></asp:TemplateField></Columns></asp:GridView></asp:Panel></div><div class="clear"></div><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><div class="formLine"><div class="formL"><font color="maroon">
There are certain parameters like [user_id] ,[open_id], [SVID] will be special parameters
which represent user id, open id (if it applies for that user) and affiliate code, which
can be used to set in above scheme. They cannot be used as parameters to be registered in
the parameter registration section.</font></div></div><div class="formBtnArea" style="margin-right: 21px;"><asp:Button ID="btnOK" class="loginbtn" ToolTip="Save" runat="server" Text="OK"
OnClick="btnOK_Click" OnClientClick="return ShowParameterPanel();" /><asp:Button ID="btnCancel" ToolTip="Cancel" CssClass="loginbtn" runat="server" Text="Cancel"
OnClick="btnCancel_Click" /></div></ContentTemplate></ajaxToolkit:TabPanel>
Now the code for dynamically adding the rows with a blank textbox and a label which should have text 'Parameter 2' for the second row, 'Parameter 3' for the 3rd row and so on.
#region Add New Rows to Grid Dynamically By P. G. C
protected void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
Label lbl = (Label)gvParameters.Rows[rowIndex].Cells[1].FindControl("lbl1");
TextBox box1 = (TextBox)gvParameters.Rows[rowIndex].Cells[2].FindControl("txtParam");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["SerialNumber"] = i + 1;
if (i == 1)
dtCurrentTable.Rows[i - 1]["Column1"] = "SessionID";
else
dtCurrentTable.Rows[i-1]["Column1"] = "Parameter " + (i).ToString();
dtCurrentTable.Rows[i-1]["Column2"] = string.Empty;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
gvParameters.DataSource = dtCurrentTable;
gvParameters.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("SerialNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dr = dt.NewRow();
dr["SerialNumber"] = 1;
dr["Column1"] = "SessionID";
dr["Column2"] = string.Empty;
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
gvParameters.DataSource = dt;
gvParameters.DataBind();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)gvParameters.Rows[rowIndex].Cells[2].FindControl("txtParam");
Label lblprev = (Label)gvParameters.Rows[rowIndex].Cells[1].FindControl("lbl1");
box1.Text = dt.Rows[i]["Column2"].ToString();
lblprev.Text = dt.Rows[i]["Column1"].ToString();
rowIndex++;
}
}
}
}
protected void btnAddRow_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
#endregionand finally
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}I have been trying to find the mistake for two days straight. Still unable to get my code working properly. What could be the reason? Where is the mistake?
The fact is when I click the button to add a new row, the label text is empty. It is only until I click it one more time to add another row, that the text appears in the label of the previous row. Needless to mention, the latest row has a label with empty text.
I'm baffled.
Please help me. This is very important to me.
Look forward to receiving all the help from our ever co-operative, sagacious forum members I can.
Many Thanks.