Good Day!
I'm creating dynamic textboxes. The number of textboxes that should be created depends on the number of column elements my XML has. I can create the textboxes, my problem is retrieving the value of each textboxes on a button click. Could you help me with this?
Here is my code for creating textboxes:
foreach (XMLClasses.column col in columns.ToList())
{
Literal lt = new Literal();
TextBox txtbox = new TextBox();
Label lbl = new Label();
lt.Text = "<br/>";
lbl.Text = col.title + ":";
lbl.Width = Unit.Pixel(200);
txtbox.ID = "txtbox_" + col.id;
txtbox.Width = Unit.Pixel(200);
Panel1.Controls.Add(lt);
Panel1.Controls.Add(lbl);
Panel1.Controls.Add(txtbox);
}
Here is my code for retrieving:(button_click)
foreach (XMLClasses.column col in columns.ToList())
{
TextBox txt = new TextBox();
Panel pnl = new Panel();
ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if(cph != null)
{
pnl = (Panel)cph.FindControl("Panel1");
if (pnl != null)
{
txt = (TextBox)pnl.FindControl("txtbox_" + col.id);
if (txt != null)
{
value = txt.Text;
}
else
{
value = "NOOOO";
}
}
}
}
I am getting the value "NOOOO". Am I missing something??
Related
Hi given below code sets focus on last image in my panel.
how do i set it to focus on first image?
I sort of understand i have to use ID of image button i create on fly. but don't know how.Please help.
var fileIdx = 0;
foreach (Tripclass Trip in TripsByTripIds )
{
fileIdx++;
ImageButton imageButton = new ImageButton(){ ID = "imageBtn" + fileIdx };
imageButton.ImageUrl = "~/" +Trip.CorridorName+"/"+Trip.Time+"/"+Trip.ImgFileName;
imageButton.Height = Unit.Pixel(100);
imageButton.Style.Add("padding", "5px");
imageButton.Width = Unit.Pixel(100);
imageButton.Click += new ImageClickEventHandler(imageButton_Click);
AMSPanel1.Controls.Add(imageButton);
AMSPanel1.Height = Unit.Pixel(860);
imageButton.Focus();
}
var fileIdx = 0;
foreach (Tripclass Trip in TripsByTripIds )
{
fileIdx++;
ImageButton imageButton = new ImageButton(){ ID = "imageBtn" + fileIdx };
imageButton.ImageUrl = "~/" +Trip.CorridorName+"/"+Trip.Time+"/"+Trip.ImgFileName;
imageButton.Height = Unit.Pixel(100);
imageButton.Style.Add("padding", "5px");
imageButton.Width = Unit.Pixel(100);
imageButton.Click += new ImageClickEventHandler(imageButton_Click);
AMSPanel1.Controls.Add(imageButton);
AMSPanel1.Height = Unit.Pixel(860);
if(fileIdx == 1)
{
imageButton.Focus();
}
}
The only thing changed is this:
From:
imageButton.Focus();
To:
if(fileIdx == 1)
{
imageButton.Focus();
}
In my ASP.Net page I need to show an HTML div who contains : Images, Text, Arrows and Connectors.
What are my "Connectors" ?
It's an ImageButton, and when the user click on this connector, the HTML div is showing a new content. This connectors are used to navigate in a TreeView.
But my problem is :
I create all my connectors (and all the HTML div content) dynamically. When the user click on the first connector the HTML div is showing new content. But on this second content, when the user click on a connector : nothing. The Click event of the ImageButton is not fired.
This is my Connector creation (on PageLoad and then on each Connector Click) :
List<Connecteur> ListConnecteur = new List<Connecteur>();
ListConnecteur = NomenclatureObj.SelectConnecteurs(DocId, ExterneData.RapidoBDDCnx);
foreach (Connecteur CeConnecteur in ListConnecteur)
{
if (CeConnecteur.FK_docversion_suivant_id != 0)
{
ImageButton ImgBtnTmp = new ImageButton();
ImgBtnTmp.Width = 30;
ImgBtnTmp.Height = 30;
ImgBtnTmp.ImageUrl = "~/images/GreenButton.png";
ImgBtnTmp.Style.Add("left", CeConnecteur.position_x_pix.ToString() + "px");
ImgBtnTmp.Style.Add("top", CeConnecteur.position_y_pix.ToString() + "px");
ImgBtnTmp.Click += new ImageClickEventHandler(ImgBtnTmp_Click);
ImgBtnTmp.CommandArgument = CeConnecteur.FK_docversion_suivant_id.ToString();
ImgBtnTmp.Style.Add("position", "absolute");
DivAffichage.Controls.Add(ImgBtnTmp);
ImgBtnTmp.CausesValidation = true;
}
}
And this is my Connector OnClick :
public void ImgBtnTmp_Click(object sender, EventArgs e)
{
ImageButton ThisBtn = sender as ImageButton;
string CommandArg = ThisBtn.CommandArgument;
int DocId = Convert.ToInt32(CommandArg);
TREEVIEW_NIVEAU++;
//DocId of the clicked connector
Session["DocId"] = DocId;
ClearDiv();
LoadDiv(DocId);
}
EDIT 1 : My whole LoadDiv() function
public void LoadDiv(int DocId)
{
#region Connecteurs
List<Connecteur> ListConnecteur = new List<Connecteur>();
ListConnecteur = NomenclatureObj.SelectConnecteurs(DocId, ExterneData.RapidoBDDCnx);
foreach (Connecteur CeConnecteur in ListConnecteur)
{
if (CeConnecteur.FK_docversion_suivant_id != 0)
{
ImageButton ImgBtnTmp = new ImageButton();
ImgBtnTmp.Width = 30;
ImgBtnTmp.Height = 30;
ImgBtnTmp.ImageUrl = "~/images/GreenButton.png";
ImgBtnTmp.Style.Add("left", CeConnecteur.position_x_pix.ToString() + "px");
ImgBtnTmp.Style.Add("top", CeConnecteur.position_y_pix.ToString() + "px");
ImgBtnTmp.Click += new ImageClickEventHandler(ImgBtnTmp_Click);
ImgBtnTmp.CommandArgument = CeConnecteur.FK_docversion_suivant_id.ToString();
ImgBtnTmp.Style.Add("position", "absolute");
DivAffichage.Controls.Add(ImgBtnTmp);
}
}
#endregion
#region Textes
List<Texte> ListTexte = new List<Texte>();
ListTexte = NomenclatureObj.SelectTextes(DocId, LANGUE_ID, ExterneData.RapidoBDDCnx);
foreach (Texte CeTexte in ListTexte)
{
Label LblText = new Label();
LblText.Text = CeTexte.contenu;
LblText.Width = CeTexte.largeur_voulue_pix;
LblText.Style.Add("left", CeTexte.position_x_pix.ToString() + "px");
LblText.Style.Add("top", CeTexte.position_y_pix.ToString() + "px");
LblText.Style.Add("position", "absolute");
DivAffichage.Controls.Add(LblText);
}
#endregion
#region Images
List<ImageNomenclature> ListImg = new List<ImageNomenclature>();
ListImg = NomenclatureObj.SelectImages(DocId, ExterneData.RapidoBDDCnx);
foreach (ImageNomenclature CetteImage in ListImg)
{
Image ImgTmp = new Image();
ImgTmp.ImageUrl = "~/Nomenclature/RAPIDO/planches/" + CetteImage.fichier_chemin;
ImgTmp.Width = CetteImage.largeur_voulue_pix;
ImgTmp.Height = CetteImage.hauteur_voulue_pix;
ImgTmp.Style.Add("left", CetteImage.position_x_pix.ToString() + "px");
ImgTmp.Style.Add("top", CetteImage.position_y_pix.ToString() + "px");
ImgTmp.Style.Add("position", "absolute");
ImgTmp.Style.Add("z-index", "-1");
DivAffichage.Controls.Add(ImgTmp);
}
#endregion
#region Flèches
List<Fleche> ListFleche = new List<Fleche>();
ListFleche = NomenclatureObj.SelectFleches(DocId, LANGUE_ID, ExterneData.RapidoBDDCnx);
foreach (Fleche CetteFleche in ListFleche)
{
string HTMLCode = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"800\" height=\"600\"><line x1=\"" + CetteFleche.position_x1_pix + "\" y1=\"" + CetteFleche.position_y1_pix + "\" x2=\"" + CetteFleche.position_x2_pix + "\" y2=\"" + CetteFleche.position_y2_pix + "\" stroke=\"#ff0000\"/></svg>";
//DivAffichage.InnerHtml += HTMLCode;
}
#endregion
}
You should create your dynamic control every time on Page_Init or Page_Load if you would like to handle events from them after Postback.
See links below for details:
http://msdn.microsoft.com/en-us/library/y3bwdsh3%28v=vs.140%29.aspx
http://msdn.microsoft.com/en-us/library/hbdfdyh7%28v=vs.100%29.aspx
Here you can see the same problem.
EDIT
Try to do something like this:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
LoadDiv(Session["DocId"])
}
}
I have a method which is intended to dynamically generate a series of divs based on the entry of a value from a dropdown list. However, I wish to reuse the same code to generate the tables on the first page_load when a number already exists.
This is where the method is called. It is called GenerateTables and it is called from the Page_Load event:
if (!IsPostBack)
{
AcademicProgramme programme;
if (Request.QueryString["id"] != null)
{
programme = academic.GetAcademicProgramme(Request.QueryString["id"]);
programmeName.Text = programme.Name;
PopulateView(programme);
GenerateTables(programme.Levels);
}
}
And here is the method itself (apologies for the size of the method):
private void GenerateTables(int count)
{
for (int i = 1; i < count + 1; i++)
{
LiteralControl title = new LiteralControl();
LiteralControl close = new LiteralControl();
LiteralControl close2 = new LiteralControl();
String script = "<div class=\"ModuleProgTable\"><h3>Level " + i + "</Modules></h3></br>";
title.Text = script;
AcademicTable.Controls.Add(title);
Panel panel = new Panel();
panel.ID = "Level" + i + "Modules";
PopulatePanel(panel, GetModulesSession(i));
Button a = new Button();
a.ID = "AddModule" + i;
a.Text = "Add Module";
a.Click += (OpenPopup);
AcademicTable.Controls.Add(panel);
AcademicTable.Controls.Add(a);
close.Text = "</div> <!-- Close here -->";
close2.Text = "</div>";
AcademicTable.Controls.Add(close);
}
}
The divs are clearly being populated because if I change the dropdown option then they appear on the PostBack without fail. It's when I try to get them to render on the first page_load that I am having problems.
Any feedback and advice would be greatly appreciated!
Regards,
-Michael
I have created a web application in which I need to generate textboxes depending upon the number the user enters.I have used Ajax and created textboxes dynamically but these textboxes are not accessible from code-behind.I have used find control but to no use.This is the code I use to generate textbox.
if (txtnobranches.Text != "")
{
if (Convert.ToInt32(txtnobranches.Text) != 0)
{
div_br.InnerHtml = "<table></table>";
tbl_br.Controls.Clear();
for (int i = 0; i < Convert.ToInt32(txtnobranches.Text); i++)
{
TableRow rowbr = new TableRow();
TableCell cellname = new TableCell();
cellname.Text = "Branch Location " + i.ToString();
rowbr.Cells.Add(cellname);
TableCell cellvalue = new TableCell();
cellvalue.Text = "";
TextBox txt = new TextBox();
txt.Text = "";
txt.ID = "txtbranchloc" + i.ToString();
cellvalue.Controls.Add(txt);
cellvalue.ID = "txtbranchloc" + i.ToString();
rowbr.Cells.Add(cellvalue);
tbl_br.Rows.Add(rowbr);
}
}
}
You can access the element by name using Request.Form["NameOfFormControl"] You will have to make little change in code.
In javascript
txt.Name = "txtbranchloc" + i.ToString();
In Code Behind
Note: Make sure you access it in postback otherwise you get null
if (Page.IsPostBack)
{
string firstTxtVal = Request.Form["txtbranchloc0"].ToString();
string secondTxtVal = Request.Form["txtbranchloc1"].ToString();
}
Using Request.Form Collection
I created a button that is supposed to view a message in a updatepanel.
I dynamically added through code since the ammount of buttons are relative to how many messages they recieve. I need the button to display a label. Any Ideas?
Here is my code:
I feel like the problem that the scope is limited to the loop. I was going to change the id to increase "lblbody" = 1+=1
$ while (reader.Read())
{
string strrecipient, strsender, strsubject, strbody, strdate, strviewstate;
strdate = "Date Sent: " + reader["date"].ToString();
strsender = "From: " + reader["sender"].ToString();
strsubject = "Subject: " + reader["subject"].ToString();
strbody = reader["body"].ToString();
strrecipient = "To: " + reader["recipient"].ToString();
if (reader["viewstate"].ToString() == "notread")
{
strviewstate = "UnRead";
}
else
{
strviewstate = "read";
}
string strName;
int intName;
intName = 0;
strName = intName.ToString();
Panel pnlNewMess = new Panel();
pnlMess.Controls.Add(pnlNewMess);
pnlNewMess.BorderColor = System.Drawing.Color.LightGray;
pnlNewMess.BorderStyle = BorderStyle.Solid;
pnlNewMess.BorderWidth = 1;
Label lbldate = new Label();
Label lblsender = new Label();
Label lblsubject = new Label();
Label lblbody = new Label();
Label lblrecipient = new Label();
Label lblviewstate = new Label();
Button btnView = new Button();
lbldate.Text = strdate;
lblsender.Text = strsender;
lblsubject.Text = strsubject;
lblbody.Text = strbody;
lblrecipient.Text = strrecipient;
lblviewstate.Text = strviewstate;
btnView.Text = "View Message";
btnView.ID = strsubject;
lblbody.Visible = false;
lblrecipient.Visible = false;
lblviewstate.Visible = false;
//lblbody.ID = "lblBody" + strName;
pnlNewMess.Controls.Add(lblrecipient);
pnlNewMess.Controls.Add(new LiteralControl("<br />"));
if (lblviewstate.Text == "notread")
{
pnlNewMess.Controls.Add(new LiteralControl("<div class='clsmess' style='background-image:url('images/unread.png'); color:white;'>"));
}
else
{
pnlNewMess.Controls.Add(new LiteralControl("<div class='clsmess' style='background-image:url('images/read.png'); color:white;'>"));
}
pnlNewMess.Controls.Add(lbldate);
pnlNewMess.Controls.Add(lblsubject);
pnlNewMess.Controls.Add(lblsender);
pnlNewMess.Controls.Add(btnView);
pnlNewMess.Controls.Add(new LiteralControl("</div>"));
pnlNewMess.Controls.Add(lblviewstate);
pnlNewMess.Controls.Add(new LiteralControl("<br />"));
pnlView.Controls.Add(lblbody);
pnlMess.Controls.Add(pnlNewMess);
}
The only thing I have tried was to set a click event for the button taking the subject lbl.text to a global variabe and then with the click of another button, would compare the subject field with the database and display the lblbody.
btnview.text = lblsubject.text;
SqlCommand CMretMess = new SqlCommand("SELECT body FROM [message] WHERE subject='" + clsGlobals.myGlobals.strSub + "'", connection);
lblBody.Text = CMretMess.ExecuteScalar().ToString();
connection.Close();
Could you do something as simple as this?
btnView.Click += (sender, e) => {
lblbody.Visible = true;
};