I have to click twice a link button to change page - asp.net

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);
}
}
}

Related

pass parameter value from one method to another aspx.net

I need help here. Below is my code. In this line of code
dtCurrentTable.Rows[0]["Kolicina"] = Convert.ToInt32(Label37.Text) + 3;
instead of 3 in next method this Label37.Text value should be incremented for one each time when user press the button.So I want this label value in next method to start to count from current value for one each time on button click.
protected void btnTest_Click(object sender, EventArgs e)
{
var clickedRow = ((Button)sender).NamingContainer as GridViewRow;
var clickedIndex = clickedRow.RowIndex;
decimal old = dtCurrentTable.Rows[clickedIndex].Field<decimal>("Kolicina");
decimal oldIznos = dtCurrentTable.Rows[clickedIndex].Field<decimal>("VkIznos");
decimal VkDanok = dtCurrentTable.Rows[clickedIndex].Field<decimal>("VkDanok");
string Cena1 = dtCurrentTable.Rows[clickedIndex].Field<string>("Cena1");
int TarifaID = dtCurrentTable.Rows[clickedIndex].Field<Int16>("TarifaID");
newValue = old - 1;
// so i need label value from here to go in next method and increment++
Label37.Text = newValue.ToString();
decimal newIznos = oldIznos - Convert.ToDecimal(Cena1);
dtCurrentTable.Rows[clickedIndex].SetField("Kolicina", newValue.ToString());
dtCurrentTable.Rows[clickedIndex].SetField("VkIznos", newIznos.ToString());
}
protected void Button6_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["string2"].ConnectionString);
conn.Open();
ViewState["count"] = Convert.ToInt32(ViewState["count"]) + 1;
// SqlCommand sqlCmd = new SqlCommand();
// sqlCmd.CommandText = "SupaSpanak";
// sqlCmd.Connection = conn;
// sqlCmd.CommandType = CommandType.StoredProcedure;
SqlCommand cmd1 = new SqlCommand("SELECT pkid,Artikal,Vid,EdMera,TarifaID,Cena1 FROM Artikli WHERE Artikal= 'N KREM SUPA OD SPANA]++' AND Cena1 = 130.00", conn);
//brojac za kolicina
count++;
decimal noofcount = count;
Session["kolicina"] = noofcount;
Label5.Text = Session["kolicina"].ToString();//ViewState["count"].ToString();//
SqlDataReader reader = cmd1.ExecuteReader();
if (reader.Read())
{
Label9.Text = (string)reader["pkid"].ToString();
Label3.Text = (string)reader["Artikal"].ToString();
Label23.Text = (string)reader["Vid"].ToString();
Label10.Text = (string)reader["EdMera"].ToString();
Label7.Text = (string)reader["TarifaID"].ToString();
Label4.Text = (string)reader["Cena1"].ToString();
decimal cena = Convert.ToDecimal(Label5.Text);//kolicina
decimal kolicina = Convert.ToDecimal(Label4.Text);//cena
//vkIznos
decimal mnoz = cena * kolicina;
Label6.Text = mnoz.ToString();
Convert.ToDecimal(Label6.Text);
conn.Close();
DataTable dtCurrentTable = (DataTable)ViewState["Markici"];
if (Label37.Text == "0")
{
dtCurrentTable.Rows[0]["Kolicina"] = Label5.Text;
}
if (Label37.Text != "0")
{
//Here this current value of Label37.text need to be incremented for one each time when button is clicked.
dtCurrentTable.Rows[0]["Kolicina"] = Convert.ToInt32(Label37.Text) + 3;
}
you need viewstate,
public int Counter
{
get
{
if (ViewState["Counter"] != null)
{
return Convert.ToInt32(ViewState["Counter"]);
}
else
return 0;
}
set { ViewState["Counter"] = value; }
}
protected void Button6_Click(object sender, EventArgs e)
{
//other codes..
Counter = Counter + 1;
if (Label37.Text != "0")
{
dtCurrentTable.Rows[0]["Kolicina"] = Convert.ToInt32(Label37.Text) + Counter;
}
}

Can't get value from dynamically created control in dynamically created foter row in grid view

I dynamically created footer row in row_created event and add dynamically created controls on button click which also dynamically created in footer I want to save data in linq table But dosen't works
here is my code I really need help
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
GridViewRow foter = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
GridViewRow foter1 = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
GridViewRow foter2 = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
GridViewRow foter3 = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
TableCell span = new TableCell();
span.Attributes.Add("rowspan", "3");
span.Attributes.Add("width", "180px");
FileUpload f = new FileUpload();
f.ID = "flupImage";
f.Attributes.Add("onchange", "ShowpImagePreview(this);");
Image img = new Image();
img.ID = "ImgPrv";
img.ClientIDMode = System.Web.UI.ClientIDMode.Static;
img.Attributes.Add("Height", "60px");
img.Attributes.Add("Width", "60px");
span.Controls.Add(img);
span.Controls.Add(new LiteralControl("<br />"));
span.Controls.Add(f);
span.HorizontalAlign = HorizontalAlign.Left;
TableCell celf = new TableCell();
Label l1 = new Label();
l1.Text = "ID ";
TextBox t1 = new TextBox();
t1.ID = "id";
celf.Controls.Add(l1);
celf.Controls.Add(t1);
celf.ColumnSpan = 4;
celf.HorizontalAlign = HorizontalAlign.Right;
TableCell celf2 = new TableCell();
Label l2 = new Label();
l2.Text = "naziv";
TextBox t2 = new TextBox();
t2.ID = "naziv";
celf2.Controls.Add(l2);
celf2.Controls.Add(t2);
celf2.ColumnSpan = 3;
celf2.HorizontalAlign = HorizontalAlign.Right;
TableCell celf3 = new TableCell();
Label l3 = new Label();
l3.Text = "opis";
TextBox t3 = new TextBox();
t3.ID = "opis";
celf3.Controls.Add(l3);
celf3.Controls.Add(t3);
celf3.ColumnSpan = 4;
celf3.HorizontalAlign = HorizontalAlign.Right;
TableCell celf4 = new TableCell();
Label l4 = new Label();
l4.Text = "cena";
TextBox t4 = new TextBox();
t4.ID = "cena";
celf4.Controls.Add(l4);
celf4.Controls.Add(t4);
celf4.ColumnSpan = 3;
celf4.HorizontalAlign = HorizontalAlign.Right;
TableCell celf5 = new TableCell();
Label l6 = new Label();
l6.Text = "kategorija";
TextBox t6 = new TextBox();
t6.ID = "kategorija";
celf5.Controls.Add(l6);
celf5.Controls.Add(t6);
celf5.ColumnSpan = 7;
celf5.HorizontalAlign = HorizontalAlign.Right;
TableCell celf6 = new TableCell();
Button Button1 = new Button();
Button1.Text = "ubaci nov";
celf6.Controls.Add(Button1);
Button1.Click += new System.EventHandler(Button1_Click);
celf6.ColumnSpan = 7;
celf6.HorizontalAlign = HorizontalAlign.Center;
foter.Cells.Add(span);
foter.Cells.Add(celf);
foter.Cells.Add(celf2);
foter1.Cells.Add(celf3);
foter1.Cells.Add(celf4);
foter2.Cells.Add(celf5);
foter3.Cells.Add(celf6);
GridView1.Controls[0].Controls.Add(foter);
GridView1.Controls[0].Controls.Add(foter1);
GridView1.Controls[0].Controls.Add(foter2);
GridView1.Controls[0].Controls.Add(foter3);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataClasses1DataContext data = new DataClasses1DataContext();
Proizvodi video = new Proizvodi();
FileUpload file = (GridView1.FooterRow.FindControl("flupImage") as FileUpload);
byte[] fileByte = file.FileBytes;
Binary bin = new Binary(fileByte);
video.slika = bin;
string prID = (GridView1.FooterRow.FindControl("id") as TextBox).Text;
int proID = Convert.ToInt32(prID);
video.proizvodID = proID;
string naziv = (GridView1.FooterRow.FindControl("naziv") as TextBox).Text;
video.naziv = naziv;
string opis = (GridView1.FooterRow.FindControl("opis") as TextBox).Text;
video.opis = opis;
decimal cena=Convert.ToDecimal( (GridView1.FooterRow.FindControl("cena") as TextBox).Text);
video.cena = cena;
string katId = (GridView1.FooterRow.FindControl("kategorija") as TextBox).Text;
video.kategorijaID = Convert.ToInt32(katId);
data.Proizvodis.InsertOnSubmit(video);
data.SubmitChanges();
GridView1.DataBind();
}
I try some solution from net but dosen't work.
please help

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%");

Adding new row to listview removes above selected dropdown Clearing

When i add the new row to the List view above selected dropdownlist gets cleared .what is the reason for this problem plz suggest me
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FirstListViewRow();
BindDataToGridviewDropdownlist();
}
}
protected void BindDataToGridviewDropdownlist()
{
DataSet dsDept = new DataSet();
dsDept.ReadXml(Server.MapPath("XMLFile2.xml"));
DataView dv = dsDept.Tables[0].DefaultView;
foreach (var list in listview1.Items)
{
if (list.ItemType == ListViewItemType.DataItem)
{
DropDownList ddf = (DropDownList)list.FindControl("ddldatatype");
ddf.DataSource = dv;
ddf.DataTextField = "value";
ddf.DataBind();
ddf.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
}
private void FirstListViewRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("OrderNo", typeof(string)));
dt.Columns.Add(new DataColumn("ColumnTitle", typeof(string)));
dt.Columns.Add(new DataColumn("Datatype", typeof(string)));
dt.Columns.Add(new DataColumn("Examples", typeof(string)));
dt.Columns.Add(new DataColumn("Options", typeof(string)));
dt.Columns.Add(new DataColumn("Delete", typeof(string)));
dr = dt.NewRow();
dt.Rows.Add(dr);
dr["OrderNo"] = 1;
Session["CurrentTable"] = dt;
listview1.DataSource = dt;
listview1.DataBind();
}
private void AddNewRow()
{
int rowIndex = 0;
if (Session["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)Session["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
Label TextBoxorder = (Label)listview1.Items[rowIndex].FindControl("txtorder");
TextBox TextBoxcolumnname = (TextBox)listview1.Items[rowIndex].FindControl("txtcolumnname");
DropDownList DropDatatype = (DropDownList)listview1.Items[rowIndex].FindControl("ddldatatype");
DropDownList Dropexample = (DropDownList)listview1.Items[rowIndex].FindControl("ddlexamples");
TextBox TextBoxoptions = (TextBox)listview1.Items[rowIndex].FindControl("txtoptions");
CheckBox Checkdel = (CheckBox)listview1.Items[rowIndex].FindControl("chkdel");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["OrderNo"] = i + 1;
// dtCurrentTable.Rows[i - 1]["Order"] = TextBoxorder.Text;
dtCurrentTable.Rows[i - 1]["ColumnTitle"] = TextBoxcolumnname.Text;
dtCurrentTable.Rows[i - 1]["Datatype"] = DropDatatype.Text;
dtCurrentTable.Rows[i - 1]["Examples"] = Dropexample.Text;
dtCurrentTable.Rows[i - 1]["Options"] = TextBoxoptions.Text;
dtCurrentTable.Rows[i - 1]["Delete"] = Checkdel.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
Session["CurrentTable"] = dtCurrentTable;
listview1.DataSource = dtCurrentTable;
listview1.DataBind();
Label txn = (Label)listview1.Items[rowIndex].FindControl("txtorder");
txn.Focus();
}
}
else
{
Response.Write("Session is null");
}
BindDataToGridviewDropdownlist();
}
protected void btnGenerate_Click(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
AddNewRow();
}
protected void btndelete_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
if (Session["CurrentTable"] != null)
{
dt = (DataTable)Session["CurrentTable"];
int j = 0;
for (int i = 0; i < listview1.Items.Count; i++)
{
ListViewDataItem items = listview1.Items[i];
CheckBox chkBox = (CheckBox)items.FindControl("chkdel");
if (chkBox.Checked == true)
{
dt.Rows.RemoveAt(j);
dt.AcceptChanges();
}
else
{
j++;
}
}
Session["CurrentTable"] = dt;
listview1.DataSource = dt;
listview1.DataBind();
BindDataToGridviewDropdownlist();
}
}
protected void btnClear_Click(object sender, EventArgs e)
{
listview1.Items.Clear();
}
}
}
Add a property DataKeyNames in this ListView like this:
<asp:listview id="listview1" runat="server" datakeynames="Datatype" xmlns:asp="#unknown"></asp:listview>
and add these lines inside BindDataToGridviewDropdownlist method:
ListViewDataItem di = (ListViewDataItem)list;
string dataType = listview1.DataKeys[di.DisplayIndex].Values[0].ToString();
ddf.SelectedValue = dataType;
here is how you BindDataToGridviewDropdownlist method would look like:
protected void BindDataToGridviewDropdownlist()
{
DataSet dsDept = new DataSet();
dsDept.ReadXml(Server.MapPath("XMLFile2.xml"));
DataView dv = dsDept.Tables[0].DefaultView;
foreach (var list in listview1.Items)
{
if (list.ItemType == ListViewItemType.DataItem)
{
DropDownList ddf = (DropDownList)list.FindControl("ddldatatype");
ddf.DataSource = dv;
ddf.DataTextField = "value";
ddf.DataBind();
ddf.Items.Insert(0, new ListItem("--Select--", "0"));
ListViewDataItem di = (ListViewDataItem)list;
string dataType = listview1.DataKeys[di.DisplayIndex].Values[0].ToString();
ddf.SelectedValue = dataType;
}
}
}

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