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;
}
}
}
}
I wonder what would be the best way to label objects (label, gridview columns, ValidatorCallout) in a asp.net page, with that information (text) are on a table in the database (the table has the id of object name Language1, language 2).
What I have today is a method that gets all the controls on the page I am opening language of the user and are logged in, and it does a query on the table by passing the "id screen" that returns all objects of the same. After this is done in a loop all controls from the screen where you check the type of control (Label, RequiredFieldValidator) to properly to set the property (.Text if Label, .ErrorMessage if RequiredFieldValidator) unfortunately it is VERY SLOW. Is there a best practice to do this?
Note: App_LocalResources (.resx) does not serve to my case.
Method:
public void RotularObjetos(Control ctl)
{
foreach (Control c in ctl.Controls)
{
if (c.GetType() == typeof(Label))
{
var dicionario = ambiente.ListaObjeto(this.Modulo, this.IdEmpresa, this.IdIdioma, this.IdUsuario,
((Label)(c)).ID);
if (dicionario == null)
{
ambiente.IncluirDicionarioObjeto(this.Modulo, this.IdIdioma, 0, ((Label)(c)).ID,
AutoWare.FrameWork.ManterAmbiente.TipoObjeto.Label.GetHashCode(),
((Label)(c)).Text,
((Label)(c)).ToolTip == string.Empty
? ((Label)(c)).Text
: ((Label)(c)).ToolTip);
}
else
{
((Label)(c)).Text = dicionario.DS_ALIAS;
((Label)(c)).ToolTip = dicionario.DS_HELP;
}
}
else if (c.GetType() == typeof(RequiredFieldValidator))
{
var dicionario = ambiente.ListaObjeto(this.Modulo, this.IdEmpresa, this.IdIdioma, this.IdUsuario,
((RequiredFieldValidator)(c)).ID);
if (dicionario == null)
{
ambiente.IncluirDicionarioObjeto(this.Modulo, this.IdIdioma, 0,
((RequiredFieldValidator)(c)).ID,
AutoWare.FrameWork.ManterAmbiente.TipoObjeto
.RequiredFieldValidator.GetHashCode(),
((RequiredFieldValidator)(c)).ErrorMessage,
((RequiredFieldValidator)(c)).ToolTip == string.Empty
? ((RequiredFieldValidator)(c)).ErrorMessage
: ((RequiredFieldValidator)(c)).ToolTip);
}
else
{
((RequiredFieldValidator)(c)).ErrorMessage = dicionario.DS_ALIAS;
((RequiredFieldValidator)(c)).ToolTip = dicionario.DS_HELP;
}
}
else if (c.GetType() == typeof(LinkButton))
{
var dicionario = ambiente.ListaObjeto(this.Modulo, this.IdEmpresa, this.IdIdioma, this.IdUsuario,
((LinkButton)(c)).ID);
if (dicionario == null)
{
ambiente.IncluirDicionarioObjeto(this.Modulo, this.IdIdioma, 0, ((LinkButton)(c)).ID,
AutoWare.FrameWork.ManterAmbiente.TipoObjeto.LinkButton
.GetHashCode(), ((LinkButton)(c)).Text,
((LinkButton)(c)).ToolTip == string.Empty
? ((LinkButton)(c)).Text
: ((LinkButton)(c)).ToolTip);
}
else
{
((LinkButton)(c)).Text = dicionario.DS_ALIAS;
((LinkButton)(c)).ToolTip = dicionario.DS_HELP;
}
}
else if (c.GetType() == typeof(ImageButton))
{
var dicionario = ambiente.ListaObjeto(this.Modulo, this.IdEmpresa, this.IdIdioma, this.IdUsuario,
((ImageButton)(c)).ID);
if (dicionario == null)
{
ambiente.IncluirDicionarioObjeto(this.Modulo, this.IdIdioma, 0, ((ImageButton)(c)).ID,
AutoWare.FrameWork.ManterAmbiente.TipoObjeto.ImageButton
.GetHashCode(), string.Empty,
((ImageButton)(c)).ToolTip == string.Empty
? string.Empty
: ((ImageButton)(c)).ToolTip);
}
else
{
((ImageButton)(c)).ToolTip = dicionario.DS_HELP;
}
}
else if (c.GetType() == typeof(CheckBox))
{
var dicionario = ambiente.ListaObjeto(this.Modulo, this.IdEmpresa, this.IdIdioma, this.IdUsuario,
((CheckBox)(c)).ID);
if (dicionario == null)
{
ambiente.IncluirDicionarioObjeto(this.Modulo, this.IdIdioma, 0, ((CheckBox)(c)).ID,
AutoWare.FrameWork.ManterAmbiente.TipoObjeto.CheckBox
.GetHashCode(), string.Empty,
((CheckBox)(c)).ToolTip == string.Empty
? string.Empty
: ((CheckBox)(c)).ToolTip);
}
else
{
((CheckBox)(c)).Text = dicionario.DS_ALIAS;
((CheckBox)(c)).ToolTip = dicionario.DS_HELP;
}
}
else if (c.GetType() == typeof(Button))
{
var dicionario = ambiente.ListaObjeto(this.Modulo, this.IdEmpresa, this.IdIdioma, this.IdUsuario,
((Button)(c)).ID);
if (dicionario == null)
{
ambiente.IncluirDicionarioObjeto(this.Modulo, this.IdIdioma, 0, ((Button)(c)).ID,
AutoWare.FrameWork.ManterAmbiente.TipoObjeto.Button.GetHashCode
(), ((Button)(c)).Text,
((Button)(c)).ToolTip == string.Empty
? ((Button)(c)).Text
: ((Button)(c)).ToolTip);
}
else
{
((Button)(c)).Text = dicionario.DS_ALIAS;
((Button)(c)).ToolTip = dicionario.DS_HELP;
}
}
else if (c.GetType() == typeof(RadioButton))
{
var dicionario = ambiente.ListaObjeto(this.Modulo, this.IdEmpresa, this.IdIdioma, this.IdUsuario,
((RadioButton)(c)).ID);
if (dicionario == null)
{
ambiente.IncluirDicionarioObjeto(this.Modulo, this.IdIdioma, 0, ((RadioButton)(c)).ID,
AutoWare.FrameWork.ManterAmbiente.TipoObjeto.RadioButton
.GetHashCode(), ((RadioButton)(c)).Text,
((RadioButton)(c)).ToolTip == string.Empty
? ((RadioButton)(c)).Text
: ((RadioButton)(c)).ToolTip);
}
else
{
((RadioButton)(c)).Text = dicionario.DS_ALIAS;
((RadioButton)(c)).ToolTip = dicionario.DS_HELP;
}
}
else if (c.GetType() == typeof(GridView))
{
var grid = ambiente.ListaObjeto(this.Modulo, this.IdEmpresa, this.IdIdioma, this.IdUsuario,
((GridView)(c)).ID);
if (grid == null)
{
ambiente.IncluirDicionarioObjeto(this.Modulo, this.IdIdioma, 0, ((GridView)(c)).ID,
AutoWare.FrameWork.ManterAmbiente.TipoObjeto.GridView
.GetHashCode(), ((GridView)(c)).ToolTip,
((GridView)(c)).ToolTip);
grid = ambiente.ListaObjeto(this.Modulo, this.IdEmpresa, this.IdIdioma, this.IdUsuario,
((GridView)(c)).ID);
foreach (DataControlField child in ((GridView)(c)).Columns)
{
var coluna = ambiente.ListaObjeto(this.Modulo, grid.ID_DICIONARIO_OBJETO, this.IdEmpresa,
this.IdIdioma, this.IdUsuario, child.HeaderText);
if (coluna == null && child.HeaderText != string.Empty)
ambiente.IncluirDicionarioObjeto(this.Modulo, this.IdIdioma, grid.ID_DICIONARIO_OBJETO,
child.HeaderText,
AutoWare.FrameWork.ManterAmbiente.TipoObjeto
.GridViewColumn.GetHashCode(), child.HeaderText,
child.HeaderText);
else if (coluna != null)
child.HeaderText = coluna.DS_ALIAS;
}
}
else
{
((GridView)(c)).ToolTip = grid.DS_HELP;
foreach (DataControlField child in ((GridView)(c)).Columns)
{
var coluna = ambiente.ListaObjeto(this.Modulo, grid.ID_DICIONARIO_OBJETO, this.IdEmpresa,
this.IdIdioma, this.IdUsuario, child.HeaderText);
if (coluna == null && child.HeaderText != string.Empty)
ambiente.IncluirDicionarioObjeto(this.Modulo, this.IdIdioma, grid.ID_DICIONARIO_OBJETO,
child.HeaderText,
AutoWare.FrameWork.ManterAmbiente.TipoObjeto
.GridViewColumn.GetHashCode(), child.HeaderText,
child.HeaderText);
else if (coluna != null)
child.HeaderText = coluna.DS_ALIAS;
}
}
}
RotularObjetos(c);
}
}
Thanks a lot.
if(ddlindentno.SelectedItem.Text != "--Select--")
{
foreach (GridViewRow row in GVFeedsaleOrder.Rows)
{
DropDownList ddl = (DropDownList)row.FindControl("ddltype");
DropDownList ddl1 = (DropDownList)row.FindControl("ddlqty");
TextBox txtnufbags = (TextBox)row.FindControl("txtnoofbags");
TextBox txtrateperkg = (TextBox)row.FindControl("txtrateperkg");
if (txtnufbags.Text.Trim() != "" && txtrateperkg.Text.Trim() != "" &&
ddl.SelectedItem.Text != "--Select--" && ddl1.SelectedItem.Text != "--Select--")
{
//CalculateTotal();
objfeedretailPL.sno = Convert.ToInt32(ddlindentno.SelectedValue);
objfeedretailPL.type = Convert.ToInt32(ddl.SelectedValue);
objfeedretailPL.quantity = Convert.ToInt32(ddl1.SelectedItem.Text);
objfeedretailPL.noofbags = Convert.ToInt32(txtnufbags.Text.ToString());
objfeedretailPL.rateperkg = Convert.ToSingle(txtrateperkg.Text.ToString());
objfeedretailPL.username = Session["username"].ToString();
int baginkgs = Convert.ToInt32(ddl1.SelectedItem.Text);
int noofbags = Convert.ToInt32(txtnufbags.Text);
float ratepkg = Convert.ToSingle(txtrateperkg.Text);
float recamt = Convert.ToSingle(lblamtrec.Text.ToString());
float totprvtotal = 0.0f;
totprvtotal = noofbags * ratepkg * ratepkg;
}
}
}
Here i calculate the first row of dynamic grid..now it's total add the next row..how can i add
that..please help me
float totprvtotal = 0.0f; // Declare a varaible
if(ddlindentno.SelectedItem.Text != "--Select--")
{
foreach (GridViewRow row in GVFeedsaleOrder.Rows)
{
DropDownList ddl = (DropDownList)row.FindControl("ddltype");
DropDownList ddl1 = (DropDownList)row.FindControl("ddlqty");
TextBox txtnufbags = (TextBox)row.FindControl("txtnoofbags");
TextBox txtrateperkg = (TextBox)row.FindControl("txtrateperkg");
if (txtnufbags.Text.Trim() != "" && txtrateperkg.Text.Trim() != "" &&
ddl.SelectedItem.Text != "--Select--" && ddl1.SelectedItem.Text != "--Select--")
{
//CalculateTotal();
objfeedretailPL.sno = Convert.ToInt32(ddlindentno.SelectedValue);
objfeedretailPL.type = Convert.ToInt32(ddl.SelectedValue);
objfeedretailPL.quantity = Convert.ToInt32(ddl1.SelectedItem.Text);
objfeedretailPL.noofbags = Convert.ToInt32(txtnufbags.Text.ToString());
objfeedretailPL.rateperkg = Convert.ToSingle(txtrateperkg.Text.ToString());
objfeedretailPL.username = Session["username"].ToString();
int baginkgs = Convert.ToInt32(ddl1.SelectedItem.Text);
int noofbags = Convert.ToInt32(txtnufbags.Text);
float ratepkg = Convert.ToSingle(txtrateperkg.Text);
float recamt = Convert.ToSingle(lblamtrec.Text.ToString());
totprvtotal = noofbags * ratepkg * ratepkg;
}
}
}
credit goes to #Ashish Charan
Pls try below...
if(ddlindentno.SelectedItem.Text != "--Select--")
{
float totprvtotal = 0.0f;
foreach (GridViewRow row in GVFeedsaleOrder.Rows)
{
DropDownList ddl = (DropDownList)row.FindControl("ddltype");
DropDownList ddl1 = (DropDownList)row.FindControl("ddlqty");
TextBox txtnufbags = (TextBox)row.FindControl("txtnoofbags");
TextBox txtrateperkg = (TextBox)row.FindControl("txtrateperkg");
if (txtnufbags.Text.Trim() != "" && txtrateperkg.Text.Trim() != "" &&
ddl.SelectedItem.Text != "--Select--" && ddl1.SelectedItem.Text != "--Select--")
{
//CalculateTotal();
objfeedretailPL.sno = Convert.ToInt32(ddlindentno.SelectedValue);
objfeedretailPL.type = Convert.ToInt32(ddl.SelectedValue);
objfeedretailPL.quantity = Convert.ToInt32(ddl1.SelectedItem.Text);
objfeedretailPL.noofbags = Convert.ToInt32(txtnufbags.Text.ToString());
objfeedretailPL.rateperkg = Convert.ToSingle(txtrateperkg.Text.ToString());
objfeedretailPL.username = Session["username"].ToString();
int baginkgs = Convert.ToInt32(ddl1.SelectedItem.Text);
int noofbags = Convert.ToInt32(txtnufbags.Text);
float ratepkg = Convert.ToSingle(txtrateperkg.Text);
float recamt = Convert.ToSingle(lblamtrec.Text.ToString());
totprvtotal = noofbags * ratepkg * ratepkg + totprvtotal;
}
}
}
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
}
}
}
}
}
}
I've been given working ASP code which I have to change in order to update existing data in database insted of creating new entries. I think I should swap INSERT INTO statment with UPDATE but the code is a bit too coplicated for me to figure out where and what to change.
Here it comes:
// *** Edit Operations: declare variables
// set the form action variable
var MM_editAction = Request.ServerVariables("SCRIPT_NAME");
if (Request.QueryString) {
MM_editAction += "?" + Server.HTMLEncode(Request.QueryString);
}
// boolean to abort record edit
var MM_abortEdit = false;
// query string to execute
var MM_editQuery = "";
// *** Insert Record: set variables
if (String(Request("MM_insert")) == "AddRecord") {
var MM_editConnection = MM_PhoneWeb_conn_STRING;
var MM_editTable = "Employees";
var MM_editRedirectUrl = "userRegister.asp";
var MM_fieldsStr = "textSurname|value|textFirstname|value|textUsername|value|textPass|value";
var MM_columnsStr = "Surname|',none,''|FirstName|',none,''|Username|',none,''|Password|',none,''";
// create the MM_fields and MM_columns arrays
var MM_fields = MM_fieldsStr.split("|");
var MM_columns = MM_columnsStr.split("|");
// set the form values
for (var i=0; i+1 < MM_fields.length; i+=2) {
MM_fields[i+1] = String(Request.Form(MM_fields[i]));
}
// append the query string to the redirect URL
if (MM_editRedirectUrl && Request.QueryString && Request.QueryString.Count > 0) {
MM_editRedirectUrl += ((MM_editRedirectUrl.indexOf('?') == -1)?"?":"&") + Request.QueryString;
}
}
// *** Insert Record: construct a sql insert statement and execute it
if (String(Request("MM_insert")) != "undefined") {
// create the sql insert statement
var MM_tableValues = "", MM_dbValues = "";
for (var i=0; i+1 < MM_fields.length; i+=2) {
var formVal = MM_fields[i+1];
var MM_typesArray = MM_columns[i+1].split(",");
var delim = (MM_typesArray[0] != "none") ? MM_typesArray[0] : "";
var altVal = (MM_typesArray[1] != "none") ? MM_typesArray[1] : "";
var emptyVal = (MM_typesArray[2] != "none") ? MM_typesArray[2] : "";
if (formVal == "" || formVal == "undefined") {
formVal = emptyVal;
} else {
if (altVal != "") {
formVal = altVal;
} else if (delim == "'") { // escape quotes
formVal = "'" + formVal.replace(/'/g,"''") + "'";
} else {
formVal = delim + formVal + delim;
}
}
MM_tableValues += ((i != 0) ? "," : "") + MM_columns[i];
MM_dbValues += ((i != 0) ? "," : "") + formVal;
}
MM_editQuery = "insert into " + MM_editTable + " (" + MM_tableValues + ") values (" + MM_dbValues + ")";
if (!MM_abortEdit) {
// execute the insert
var MM_editCmd = Server.CreateObject('ADODB.Command');
MM_editCmd.ActiveConnection = MM_editConnection;
MM_editCmd.CommandText = MM_editQuery;
MM_editCmd.Execute();
MM_editCmd.ActiveConnection.Close();
if (MM_editRedirectUrl) {
Response.Redirect(MM_editRedirectUrl);