HOw can I handle Dynamically generated Controls - asp.net

In the following image the table displayed is generated dynamically on click of “Create Table” button.
I have added textboxes, fileupload, Buttons dynamically to the table.
I want to upload files from fileupload control on click of “Upload” button in the Table, but I have no idea how to handle those Dynamically generated Controls.
Code of “Create Table” button goes like this :
protected void Button1_Click(object sender, EventArgs e)
{
//Button1.Visible = false;
//Creat the Table and Add it to the Page
Table table = new Table();
table.Caption = "Table1";
table.BackColor = System.Drawing.Color.BurlyWood;
Page.Form.Controls.Add(table);
for (int i = 0; i < 3; i++)
{
TableRow row = new TableRow();
row.BorderStyle = BorderStyle.Ridge;
for (int j = 0; j <= 10; j++)
{
TableCell cell = new TableCell();
cell.BorderWidth = 5;
cell.BorderStyle = BorderStyle.Ridge;
cell.BorderColor = System.Drawing.Color.Azure;
for (j = 0; j <= 0; j++)
{
Label lbl = new Label();
lbl.ID = "lblCCRow" + i + "Col" + j;
lbl.Text = "CC NO. " + i + " ";
lbl.Width = 100;
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (j = 1; j <= 1; j++)
{
Label lbl = new Label();
lbl.ID = "lblRow" + i + "Col" + j;
lbl.Width = 100;
lbl.Text = Convert.ToString(DateTime.Now.Day) + "/" + Convert.ToString(DateTime.Now.Month) + "/" + Convert.ToString(DateTime.Now.Year);
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
for (j = 2; j <= 7; j++)
{
TextBox tb = new TextBox();
tb.Width = 100;
tb.ID = "txtBoxRow" + i + "Col" + j;
tb.Text = "";
// Add the control to the TableCell
cell.Controls.Add(tb);
}
for (j = 8; j <= 8; j++)
{
FileUpload fileUp = new FileUpload();
fileUp.ID = "flupRow" + i + "Col" + j;
fileUp.Width = 220;
cell.Controls.Add(fileUp);
}
for (j = 9; j <= 9; j++)
{
Button btnUpld = new Button();
btnUpld.Width = 100;
btnUpld.ID = "btnUpRow" + i + "Col" + j;
btnUpld.Text = "Upload";
cell.Controls.Add(btnUpld);
}
for (j = 10; j <= 10; j++)
{
Label lbl = new Label();
lbl.ID = "lblRow" + i + "Col" + j;
lbl.Text = "[ Status ]";
lbl.Width = 100;
// Add the control to the TableCell
cell.Controls.Add(lbl);
}
row.Cells.Add(cell);
}
// Add the TableRow to the Table
table.Rows.Add(row);
}
//table.Rows.Add(row);
}

Attach event handeler btnUpld runtime.
Button btnUpld = new Button();
btnUpld.Width = 100;
btnUpld.ID = "btnUpRow" + i + "Col" + j;
btnUpld.Text = "Upload";
btnUpld.Click += new EventHandler(btnUpld_Click);
cell.Controls.Add(btnUpld);
//code behind
private void btnUpld_Click(object sender, System.EventArgs e)
{
// Add upload functionality here
}

btnUpload.Click +=new EventHandler(btnUpload_Click);
add an event handler after adding the control dynamically to the form, using the above code and then add create a function that handles this event using the following code
protected void btnEdit_Click(object sender, EventArgs e)
{
}

Related

How to read each row of grid

I am trying to use a tool tip on every row of a grid to show details whenever user place pointer on particular cell. It's supposed to show details for every row but it only shows details for the first row. Can any one help me?
for (int i = 1; i <= e.Row.Cells.Count - 1; i++)
{
if (e.Row.Cells[i].Text == "0" || string.IsNullOrEmpty(e.Row.Cells[i].Text) || e.Row.Cells[i].Text == " ")
{
e.Row.Cells[i].Text = "";
}
else
{
e.Row.Cells[i].BackColor = System.Drawing.Color.Blue;
dateSetExport.Tables.Clear();
dateSetExport.Reset();
SqlParameter[] param = new SqlParameter[2];
param[1] = new SqlParameter("#Startdate", gvDetails.HeaderRow.Cells[i].Text);
param[0] = new SqlParameter("#Employe_Id", e.Row.Cells[0].Text.Split('-')[0]);
DataTable dt1 = DataHelper.getDataTableExecuteSP("usp_GetToolTip", param);
dt1.TableName = "ToolTip";
dateSetExport.Tables.Add(dt1);
string tooltip = "";
for (int j = 0; j < dt1.Rows.Count; j++)
{
tooltip = tooltip + dt1.Rows[j]["normal_working_hours"].ToString() + " Hours : " + dt1.Rows[j]["description"].ToString()+"\n\n";
}
e.Row.Cells[i].ToolTip = tooltip;
}
}
You can use another foreach in order to loop all rows :
foreach (GridViewRow row in GridView.Rows)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//do your staff
}
}
But for me, you should use GridView_RowDataBound event :
protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 1; i <= e.Row.Cells.Count - 1; i++)
{
if (e.Row.Cells[i].Text == "0" || string.IsNullOrEmpty(e.Row.Cells [i].Text) || e.Row.Cells[i].Text == " ")
{
e.Row.Cells[i].Text = "";
}
else
{
e.Row.Cells[i].BackColor = System.Drawing.Color.Blue;
dateSetExport.Tables.Clear();
dateSetExport.Reset();
SqlParameter[] param = new SqlParameter[2];
param[1] = new SqlParameter("#Startdate", gvDetails.HeaderRow.Cells[i].Text);
param[0] = new SqlParameter("#Employe_Id", e.Row.Cells[0].Text.Split('-')[0]);
DataTable dt1 = DataHelper.getDataTableExecuteSP("usp_GetToolTip", param);
dt1.TableName = "ToolTip";
dateSetExport.Tables.Add(dt1);
string tooltip = "";
for (int j = 0; j < dt1.Rows.Count; j++)
{
tooltip = tooltip + dt1.Rows[j]["normal_working_hours"].ToString() + " Hours : " + dt1.Rows[j]["description"].ToString()+"\n\n";
}`enter code here`
e.Row.Cells[i].ToolTip = tooltip;
}
}
}
}

Grid view event not getting triggered

I have a gridview created dynamically , now i am trying to do edit each column by keeping a link button. But the rowdatabound event is not getting triggered.Where i might have gone wrong?
Below is my code :
dtValues = gObj.GetAllDocumentsHistoryList();
dtHeader = gObj.GetAllHeaderList();
GridView gvEmployee = new GridView();
gvEmployee.ShowHeaderWhenEmpty = true;
gvEmployee.EmptyDataText = "Sorry No History Records Found !!!!!!!";
gvEmployee.AutoGenerateColumns = false;
for (int i = 0; i < dtValues.Columns.Count; i++)
{
string name = dtValues.Columns[i].ColumnName.ToString();
BoundField boundfield = new BoundField();
boundfield.DataField = dtValues.Columns[i].ColumnName.ToString();
for (int j = 0; j < dtHeader.Rows.Count; j++)
{
if (dtHeader.Rows[j]["ColCode"].ToString() == dtValues.Columns[i].ColumnName.ToString())
{
boundfield.HeaderText = dtHeader.Rows[j]["ColName"].ToString();
if (boundfield.HeaderText.Contains("Date") || boundfield.HeaderText.Contains("DocExpiry"))
{
boundfield.DataFormatString = "{0:dd/MMM/yyyy}";
}
}
else if (dtValues.Columns[i].ColumnName.ToString() == "Last Modified Date")
{
boundfield.HeaderText = "Last Modified Date";
boundfield.DataFormatString = "{0:dd/MMM/yyyy}";
}
}
gvEmployee.Columns.Add(boundfield);
}
gvEmployee.DataSource = dtValues;
gvEmployee.DataBind();
gvEmployee.Width = new Unit("90%");
gvEmployee.RowDataBound += new GridViewRowEventHandler(gvEmployee_RowDataBound);
protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButton lnkView = new LinkButton();
lnkView.ID = "lnkView";
lnkView.Text = "View";
lnkView.Click += ViewDetails;
lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row["Id"].ToString();
e.Row.Cells[3].Controls.Add(lnkView);
}
Just add your RowDataBound handler before DataBind.
Corrected Code:
gvEmployee.DataSource = dtValues;
gvEmployee.RowDataBound += new GridViewRowEventHandler(gvEmployee_RowDataBound);
gvEmployee.DataBind();
gvEmployee.Width = new Unit("90%");

I have to click twice a link button to change page

I have to click linkbutton twice to change the page and load subfolders of current folder. I think I have some problems with my session and event.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TableRow r = new TableRow();
TableCell c1 = new TableCell();
LinkButton l = new LinkButton();
var strarray = Server.MapPath(Session["url"].ToString()).Split(Path.DirectorySeparatorChar);
Table table = new Table();
l.ID = "lable" + table.Rows.Count;
l.Text = strarray.Last();
l.EnableViewState = true;
r.ID = "newRow" + table.Rows.Count;
c1.ID = "newC1" + table.Rows.Count;
c1.Controls.Add(l);
r.Cells.Add(c1);
table.Rows.Add(r);
this.form1.Controls.Add(table);
}
else
{
if(Directory.Exists(Server.MapPath(Session["url"].ToString())))
{
string[] allFolders = Directory.GetDirectories(Server.MapPath(Session["url"].ToString()));
Table table = new Table();
foreach (string str in allFolders)
{
TableRow r = new TableRow();
TableCell c1 = new TableCell();
LinkButton l = new LinkButton();
var strarray = str.Split(Path.DirectorySeparatorChar);
l.ID = strarray.Last();
l.CommandName = "createLink";
l.Click+= new EventHandler(LinkButton_Command);
l.Text = strarray.Last();
l.EnableViewState = true;
r.ID = "newRow" + table.Rows.Count;
c1.ID = "newC1" + table.Rows.Count;
c1.Controls.Add(l);
r.Cells.Add(c1);
table.Rows.Add(r);
this.form1.Controls.Add(table);
}
}
else
{
Work work=new Work();
work.SetFolder(Session["url"].ToString());
work.CreateFolder();
}
}
}
public void LinkButton_Command(object sender, EventArgs e)
{
Session["url"] = Session["url"] + "\\" + ((LinkButton)sender).ID;
}
I think this is because the Page_Load() method is called before the LinkButton_Command(). The session variable Session["url"] still contains the previous value when the code in Page_Load() executes.
Try moving the code from the else-branch of Page_Load() at the end of LinkButton_Command().
after mix up your answer and my program , come to a answer that will work.
protected void Page_Load(object sender, EventArgs e)
{
if (Directory.Exists(Server.MapPath(Session["url"].ToString())))
{
string[] allFolders = Directory.GetDirectories(Server.MapPath(Session["url"].ToString()));
Table table = new Table();
foreach (string str in allFolders)
{
TableRow r = new TableRow();
TableCell c1 = new TableCell();
LinkButton l = new LinkButton();
var strarray = str.Split(Path.DirectorySeparatorChar);
l.ID = strarray.Last();
l.CommandName = "createLink";
l.Click += new EventHandler(LinkButton_Command);
l.Text = strarray.Last();
l.EnableViewState = true;
r.ID = "newRow" + table.Rows.Count;
c1.ID = "newC1" + table.Rows.Count;
c1.Controls.Add(l);
r.Cells.Add(c1);
table.Rows.Add(r);
this.form1.Controls.Clear();
this.form1.Controls.Add(table);
}
}
}
public void LinkButton_Command(object sender, EventArgs e)
{
Session["url"] = Session["url"] + "\\" + ((LinkButton)sender).ID;
if (Directory.Exists(Server.MapPath(Session["url"].ToString())))
{
string[] allFolders = Directory.GetDirectories(Server.MapPath(Session["url"].ToString()));
Table table = new Table();
foreach (string str in allFolders)
{
TableRow r = new TableRow();
TableCell c1 = new TableCell();
LinkButton l = new LinkButton();
var strarray = str.Split(Path.DirectorySeparatorChar);
l.ID = strarray.Last();
l.CommandName = "createLink";
l.Click += new EventHandler(LinkButton_Command);
l.Text = strarray.Last();
l.EnableViewState = true;
r.ID = "newRow" + table.Rows.Count;
c1.ID = "newC1" + table.Rows.Count;
c1.Controls.Add(l);
r.Cells.Add(c1);
table.Rows.Add(r);
this.form1.Controls.Clear();
this.form1.Controls.Add(table);
}
}
}

Adding eventHandler in each label from the code behind for Pager numbers

My page structure is like this:
<div style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; OVERFLOW-Y: auto; WIDTH: 100%; ; HEIGHT: expression(document.body.clientHeight-270); BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid"id="divGrid">
<asp:datalist id="dlResults" runat="server" Width="100%" CellSpacing="0" CellPadding="0" RepeatDirection="Vertical">
...
</asp:datalist>
</div>
<table class="bodytext8pt" border="0" width="100%">
<tr>
<td><asp:panel id="pnlPager" Runat="server" CssClass="GridFooter"></asp:panel></td>
</tr>
</table>
In the code behind pager is build by the BuildPager function:
private void BuildPager(DataTable dt)
{
pnlPager.Controls.Clear();
Label l = new Label();
l.Text = " (" + pgResults.PageCount.ToString("#,##0") + " pages, " + dt.Rows.Count.ToString("#,##0") + " records ) ";
pnlPager.Controls.Add(l);
for (int i = 0; i < pnlPager.Controls.Count; i++)
{
if (pnlPager.Controls[i].ToString() == "System.Web.UI.WebControls.DataGridLinkButton")
{
try
{
LinkButton c = (LinkButton) pnlPager.Controls[i];
c.CssClass = "GridFooter";
}
catch (Exception ex) { }
}
}
pnlPager.Attributes.Add("class", "GridFooter");
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(0, l);
LinkButton lb = null;
lb = new LinkButton();
lb.Text = "Previous";
lb.Attributes.Add("OnClick", "goPage(" + Convert.ToString(Convert.ToInt32(pgResults.CurrentPageIndex) - 1) + ");");
lb.CommandArgument = Convert.ToString(Convert.ToInt32(pgResults.CurrentPageIndex) - 1);
if (pgResults.CurrentPageIndex == 0)
{
lb.Enabled = false;
lb.Style.Add("cursor", "default");
}
else
{
lb.Attributes.Add("class", "GridFooter");
}
**lb.Click += new EventHandler(Pager_Click);
pnlPager.Controls.AddAt(0, lb);**
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(0, l);
lb = new LinkButton();
lb.Text = "First";
lb.Attributes.Add("OnClick", "goPage(0);");
lb.CommandArgument = Convert.ToString(0);
if (pgResults.CurrentPageIndex == 0)
{
lb.Enabled = false;
lb.Style.Add("cursor", "default");
}
else
{
lb.Attributes.Add("class", "GridFooter");
}
**lb.Click += new EventHandler(Pager_Click);
pnlPager.Controls.AddAt(0, lb);**
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(0, l);
//Build the numeric links..
for(int i=0; i< pgResults.PageCount; i++)
{
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, l);
lb = new LinkButton();
lb.Text = Convert.ToString(i+1);
lb.Attributes.Add("OnClick", "goPage(" + Convert.ToString(i) + ");");
lb.CommandArgument = Convert.ToString(i);
if (pgResults.CurrentPageIndex == i)
{
lb.Enabled = false;
lb.Style.Add("cursor", "default");
}
else
{
lb.Attributes.Add("class", "GridFooter");
**lb.Click += new EventHandler(Pager_Click);**
}
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, lb);
}
//End of numeric links
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, l);
lb = new LinkButton();
lb.Text = "Next";
lb.Attributes.Add("OnClick", "goPage(" + Convert.ToString(Convert.ToInt32(pgResults.CurrentPageIndex) + 1) + ");");
lb.CommandArgument = Convert.ToString(Convert.ToInt32(pgResults.CurrentPageIndex) + 1);
if (pgResults.CurrentPageIndex == pgResults.PageCount - 1)
{
lb.Enabled = false;
lb.Style.Add("cursor", "default");
}
else
{
lb.Attributes.Add("class", "GridFooter");
}
**lb.Click += new EventHandler(Pager_Click);**
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, lb);
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, l);
lb = new LinkButton();
lb.Text = "Last";
lb.Attributes.Add("OnClick", "goPage(" + Convert.ToString(Convert.ToInt32(pgResults.PageCount) - 1) + ");");
lb.CommandArgument = Convert.ToString(Convert.ToInt32(pgResults.PageCount) - 1);
if (pgResults.CurrentPageIndex == pgResults.PageCount - 1)
{
lb.Enabled = false;
lb.Style.Add("cursor", "default");
}
else
{
lb.Attributes.Add("class", "GridFooter");
}
**lb.Click += new EventHandler(Pager_Click);**
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, lb);
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, l);
}
Number labels are added with the clicking events:
private void Pager_Click(object sender, EventArgs e)
{
LinkButton lb = (LinkButton) sender;
pgResults.CurrentPageIndex = Convert.ToInt32(lb.CommandArgument);
BindList(false);
}
But when i am clicking these numbers or 'next','previous' this pager_click is not firing.
Have i added eventhandlers in a correct way.
Please suggest
Thanks
Found out the reasion.
Dynamically created controls are lost on the page load,
so we need to again load them in postback.

Find radio button control in nested repeater control in asp.net?

I have two repeater controls one inside other and in inner repeater there is placeholder in which radio button is dynamically generated. I want to find the radio button control to check whether it is checked or not? I want above all function to be performed in button submit/click event defined in the code?
if (!Page.IsPostBack)
{
//1) Load SomeDatatable from Database somehow
// Just for testing : replace with query to DB
strqry = "select * from Quiz_tblQsnsLimitMaster where Qsnexamname='" + Request.QueryString["QsnEname"].ToString() + "'";
SqlDataAdapter adp = new SqlDataAdapter(strqry, sCon);
DataSet ds = new DataSet();
try
{
adp.Fill(ds);
int total = ds.Tables[0].Rows.Count;
for (int i = 0; i < total; i++)
{
string QuesID = ds.Tables[0].Rows[i].ItemArray[1].ToString();
//SubName = ds.Tables[0].Rows[i].ItemArray[3].ToString();
DataSet oDs = SqlHelper.ExecuteDataset(sCon, "Ps_Quiz_OnlineTest_QuestionsWithOptions_Get", QuesID);
SomeDatatable.Merge(oDs.Tables[0]);
}
removeDuplicatesRows(SomeDatatable);
System.Data.DataColumn newColumn = new System.Data.DataColumn("ContentIndex", typeof(System.String));
newColumn.DefaultValue = "0";
SomeDatatable.Columns.Add(newColumn);
for (int i = 0; i < Math.Ceiling((decimal)SomeDatatable.Rows.Count); i++)
SomeDatatable.Rows[i]["ContentIndex"] = i + 1;
}
catch
{
}
////2) Create a dummy data source for the tab repeater using a list of anonymous types
List<object> TabList = new List<object>();
//BindSubject();
for (int I = 0; I < Math.Ceiling((decimal)SomeDatatable.Rows.Count / (decimal)ContentPerTab); I++)
{
TabList.Add(new { TabIndex = I });
}
TabRepeater.ItemDataBound += TabRepeater_ItemDataBound;
TabRepeater.DataSource = TabList;
TabRepeater.DataBind();
//TablLinkRepeater.DataSource = TabList;
//TablLinkRepeater.DataBind();
//}
}
public void removeDuplicatesRows(DataTable dt)
{
SomeDatatable = dt.DefaultView.ToTable(true, "QuestionId");
}
protected void TabRepeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
int TabIndex = -1;
int.TryParse(DataBinder.Eval(e.Item.DataItem, "TabIndex").ToString(), out TabIndex);
//Copy Content Rows from SomeDatable that belong to this tab
DataTable Dt = SomeDatatable.Clone();
for (Int32 i = TabIndex * ContentPerTab; i <= (TabIndex + 1) * ContentPerTab - 1; i++)
{
if (i >= SomeDatatable.Rows.Count) break;
Dt.ImportRow(SomeDatatable.Rows[i]);
}
// Find the content repeater in this item and use the new datatable as source
Repeater ContentRepeater = (Repeater)e.Item.FindControl("ContentRepeater");
ContentRepeater.ItemDataBound += ContentRepeater_ItemDataBound;
ContentRepeater.DataSource = Dt;
ContentRepeater.DataBind();
}
}
// This handler might be needed for content repeater, included just for testing
protected void ContentRepeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
//Read coulmn from Datarow
int ContentIndex = -1;
int.TryParse(DataBinder.Eval(e.Item.DataItem, "ContentIndex").ToString(), out ContentIndex);
var parsed = "'" + HttpUtility.ParseQueryString(DataBinder.Eval(e.Item.DataItem, "QuestionID").ToString()) + "'";
//Add Question
DataSet ds = SqlHelper.ExecuteDataset(sCon, "Ps_Quiz_QuestionsWithOptions_Get", Convert.ToString(parsed));
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
int iCnt = 0;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Table tblQsn = new Table();
//.....Begin Text Qsn Creation.....//
tblQsn.Width = new Unit("98%");
TableRow trQsn = new TableRow();
iRowCounter++;
trQsn.ID = "Row_" + iRowCounter.ToString();
TableCell tcQsn = new TableCell();
TableCell tcQsnSNo = new TableCell();
tcQsn.CssClass = "Label";
tcQsn.BackColor = System.Drawing.Color.Gainsboro;
tcQsn.Font.Bold = true;
tcQsn.Font.Size = 12;
tcQsn.Text = ds.Tables[0].Rows[i][1].ToString();
tcQsn.Width = Unit.Percentage(99.5);
iCellCounter++;
tcQsn.ID = "Cell_" + iCellCounter.ToString();
tcQsnSNo.CssClass = "Label";
tcQsnSNo.Attributes.Add("valign", "top");
tcQsnSNo.BackColor = System.Drawing.Color.Gainsboro;
tcQsnSNo.Font.Bold = true;
tcQsnSNo.Width = Unit.Percentage(0.5);
iCellCounter++;
tcQsnSNo.ID = "Cell_" + iCellCounter.ToString();
iCnt++;
tcQsnSNo.Text = ContentIndex.ToString() + ".";
trQsn.Cells.Add(tcQsnSNo);
trQsn.Cells.Add(tcQsn);
tblQsn.Rows.Add(trQsn);
int rcnt = i;
int iOptCnt = 0;
string sStatus = "N";
while ((rcnt >= 0) && (rcnt < ds.Tables[0].Rows.Count))
{
if (ds.Tables[0].Rows[rcnt][2].ToString() == ds.Tables[0].Rows[i][2].ToString())
{
if (sStatus == "N")
{
sStatus = "Y";
}
TableRow trQsnOpt = new TableRow();
iRowCounter++;
trQsnOpt.ID = "Row_" + iRowCounter.ToString();
TableCell tcQsnOpt = new TableCell();
tcQsnOpt.CssClass = "Label";
iCellCounter++;
tcQsnOpt.ID = "Cell_" + iCellCounter.ToString();
tcQsnOpt.Attributes.Add("valign", "top");
tcQsnOpt.VerticalAlign = VerticalAlign.Middle;
TableCell tcQsnOptSNo = new TableCell();
tcQsnOptSNo.CssClass = "Label";
iCellCounter++;
tcQsnOptSNo.ID = "Cell_" + iCellCounter.ToString();
iOptCnt++;
RadioButton oRbOptions = new RadioButton();
oRbOptions.GroupName = ds.Tables[0].Rows[rcnt][2].ToString() + "_Group";
oRbOptions.Text = ds.Tables[0].Rows[rcnt][3].ToString().Trim();
oRbOptions.Font.Size = 11;
iRbTCounter++;
oRbOptions.ID = ds.Tables[0].Rows[i][0].ToString() + "_" + ds.Tables[0].Rows[rcnt][2].ToString() + "_" + "Option" + iOptCnt.ToString() + "_" + iRbTCounter.ToString();
//oRbOptions.Enabled = false;
//if (ds.Tables[0].Rows[i][2].ToString() == "Option" + iRbTCounter.ToString())
//{
// oRbOptions.Checked = true;
//}
oRbOptions.InputAttributes.Add("data-info", Convert.ToString(ContentIndex));
oRbOptions.CssClass = "Label";
tcQsnOpt.Controls.Add(oRbOptions);
tcQsnOptSNo.Text = iOptCnt.ToString() + ".";
trQsnOpt.Cells.Add(tcQsnOptSNo);
trQsnOpt.Cells.Add(tcQsnOpt);
rcnt++;
//.....Add Option Image.....//
tblQsn.Rows.Add(trQsnOpt);
}
else
break;
}
i = rcnt - 1;
PlPreview = (PlaceHolder)e.Item.FindControl("PlPreview");
PlPreview.Controls.Add(tblQsn);
}
}
}
}
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
SubmitQuestion();
}
public void SubmitQuestion()
{
for (int c = 0; c < SomeDatatable.Rows.Count; c++)
{
string sQsnOptId = SomeDatatable.Rows[c]["QuestionID"].ToString();
DataSet ds = SqlHelper.ExecuteDataset(sCon, "Ps_Quiz_QuestionsWithOptions_Get", "'" + sQsnOptId + "'");
for (int i = 1; i <= 4; i++)
{
string rdboption = ds.Tables[0].Rows[c][0].ToString() + "_" + ds.Tables[0].Rows[i-1][2].ToString() + "_" + "Option" + i + "_" + i;
Repeater rpt = (Repeater)TabRepeater.NamingContainer.FindControl("ContentRepeater");
RadioButton rbt = (RadioButton)rpt.NamingContainer.FindControl(rdboption);
if (rbt.Checked)
{
strqry = "update Quiz_tblOnlineTest_Detail set UserAns='Option" + i + "', DeletionStatus='A' where CreationLogInId='AMITSAMBYAL#HOTMAIL.COM' and ExamName='" + Request.QueryString["QsnEname"].ToString() + "' and QsnId=" + Session["quesid"].ToString() + "";
cmd = new SqlCommand(strqry, con);
try
{
cmd.ExecuteNonQuery();
}
catch
{
}
finally
{
con.Close();
}
break;
}
}
}
}
}
I found the answer. Thanks
foreach (RepeaterItem repeater in TabRepeater.Items)
{
Repeater repeater1 = (Repeater)repeater.FindControl("ContentRepeater");
foreach (RepeaterItem repItem in repeater1.Items)
{
for (int i = 1; i <= 4; i++)
{
string rdboption = ds.Tables[0].Rows[c][0].ToString() + "_" + ds.Tables[0].Rows[i - 1][2].ToString() + "_" + "Option" + i + "_" + i;
PlaceHolder PlPreview = (PlaceHolder)repItem.FindControl("PlPreview");
rbt = (RadioButton)PlPreview.FindControl(rdboption);
if (rbt.Checked)
{
// statement
}
}
}
}
}
}

Resources