ImageButton not firing on the second time - c#

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"])
}
}

Related

How to set focus on first image in my Panel?

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

Pop-up window not working

I have a .net page which needs to open a new pop-up window when certain conditions are not met in the code behind file. I have the following code:
private bool isValidPart(string partNo)
{
if (!string.IsNullOrEmpty(partNo))
{
DataBase.DBManager dm = new DBManager();
if (!Convert.ToBoolean(dm.ExecScalar("usp_getPart", partNo)))
{
string url = "test.aspx";
string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";
ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
return false;
}
}
return true;
}
I put a break point and verified it. It hits the line but the pop-up window does not open up. It just simply moves to next line and returns false.
May I please know the reason behind it?
#yog2411 This is the code which checks that isvalidpart()
private bool SetRowData()
{
int rowIndex = 0;
if (ViewState["CurrentData"] != null)
{
DataTable dtCurrentData = (DataTable)ViewState["CurrentData"];
DataRow drCurrentRow = null;
if (dtCurrentData.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentData.Rows.Count; i++)
{
TextBox TextCustomerName = (TextBox)gvInventory.Rows[rowIndex].Cells[1].FindControl("txtCustomerName");
TextBox TextPONumber = (TextBox)gvInventory.Rows[rowIndex].Cells[2].FindControl("txtPONumber");
TextBox TextPartNumber = (TextBox)gvInventory.Rows[rowIndex].Cells[3].FindControl("txtPartNumber");
TextBox TextQuantity = (TextBox)gvInventory.Rows[rowIndex].Cells[4].FindControl("txtQuantity");
//TextBox TextReqShipDate = (TextBox)gvInventory.Rows[rowIndex].Cells[5].FindControl("txtReqShipDate");
if (!isValidPart(TextPartNumber.Text))
return false;
drCurrentRow = dtCurrentData.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentData.Rows[i - 1]["CustName"] = TextCustomerName.Text;
dtCurrentData.Rows[i - 1]["PONum"] = TextPONumber.Text;
dtCurrentData.Rows[i - 1]["PartNum"] = TextPartNumber.Text;
dtCurrentData.Rows[i - 1]["Qty"] = TextQuantity.Text;
// dtCurrentData.Rows[i - 1]["ReqShipDate"] = TextReqShipDate.Text;
rowIndex++;
}
ViewState["CurrentData"] = dtCurrentData;
gvInventory.DataSource = dtCurrentData;
gvInventory.DataBind();}
}
SetPreviousData();
return true;
}
try writing a JS function
function showMyPopUp(myUrl) {
//I have this settings and it works like a popUp,
//I just going to write the properties you have, but you can change them for these ones
//var CustomFeatures = 'titlebar=no, status=no,menubar=no,resizable=no,scrollbars=no,toolbar=no,location=no,width=300,height=100,top=100,left=100';
var CustomFeatures = 'resizable=yes,width=300,height=100,top=100,left=100';
window.open(myUrl, '_blank', CustomFeatures, true);
}
and in your C# this
string url = "test.aspx";
string myCallfunction = "showMyPopUp('" + url + "');"
ScriptManager.RegisterStartupScript(this, this.GetType(), "Funct", myCallfunction , true);
hope it helps

Image is not displaying in browser

hey guys i am making an art website for my fiance but cant get the images to display and i am doing it exactly like in previews websites i made but using visual studio 2012 from 2008. Here is the code. I get the error sign in the image and not the correct image. It loads the path from the database witch is Art/1.jpg.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Home : System.Web.UI.Page
{
int Add = 1;
protected void Page_Init(object sender, EventArgs e)
{
Database s = new Database();
int Total = s.ArtCount(); //Count the total of cars int the database
for (int loop = Total; loop > 0; loop--)
{
Panel Panel1 = new Panel(); //Create a new panel for all the cars
Panel1.ID = "pnl" + loop.ToString();
Panel1.Style["position"] = "absolute";
Panel1.Style["left"] = "155px";
Panel1.Style["top"] = "400px";
Panel1.Style["Height"] = "200px";
Panel1.Style["Width"] = "1000px";
Panel1.BackColor = System.Drawing.Color.Black;
form1.Controls.Add(Panel1);
if (Add >= 2)
{
int Top = (400 * (Add - 1));
Panel1.Style["top"] = (250 + Top + 20).ToString() + "px"; //Set the second and more panels down
}
Add++;
string Art = s.LoadArt(loop);
string ArtID = Art.Substring(0, Art.IndexOf("|"));
Art = Art.Substring(ArtID.Length + 1);
string ArtName = Art.Substring(0, Art.IndexOf("|"));
Art = Art.Substring(ArtName.Length + 1);
string Description = Art.Substring(0, Art.IndexOf("|"));
Art = Art.Substring(Description.Length + 1);
string PicUrl = Art.Substring(0, Art.IndexOf("|"));
string path = Server.MapPath(PicUrl);
Image image1 = new Image();
image1.ID = "img" + ArtID +loop;
image1.ImageAlign = ImageAlign.AbsMiddle;
image1.Visible = true;
image1.ImageUrl = path;
image1.Style["Left"] = "10px";
image1.Style["position"] = "absolute";
image1.Style["top"] = "10px";
image1.Height = 180;
image1.Width = 230;
Panel1.Controls.Add(image1);
Label label1 = new Label(); //Create a label for the description of each car
label1.ID = "lblDescription" + ArtID;
label1.Text = Description;
label1.ForeColor = System.Drawing.Color.Lime;
label1.BorderStyle = BorderStyle.Groove;
label1.BorderColor = System.Drawing.Color.Violet;
label1.Style["Left"] = "500px";
label1.Style["position"] = "absolute";
label1.Style["top"] = "30px";
Panel1.Controls.Add(label1);
string View = s.TimesView(ArtID);
Label label10 = new Label(); //Create a label for the times each car is viewed
label10.ID = "lblView" + ArtID+loop;
label10.Text = "Times Viewed: " + View;
label10.ForeColor = System.Drawing.Color.Lime;
label10.Style["Left"] = "799px";
label10.Style["position"] = "absolute";
label10.Style["top"] = "170px";
Panel1.Controls.Add(label10);
Button btnView = new Button(); //Create a button to view a car for all cars
btnView.ID = ArtID;
btnView.Text = "View";
btnView.ForeColor = System.Drawing.Color.DeepSkyBlue;
btnView.BackColor = System.Drawing.Color.Gray;
btnView.BorderColor = System.Drawing.Color.Violet;
btnView.Style["top"] = "150px";
btnView.Style["left"] = "800px";
btnView.Style["Height"] = "20px";
btnView.Style["position"] = "absolute";
btnView.BorderStyle = BorderStyle.Outset;
btnView.Command += new CommandEventHandler(btnView_Command); //Create a command EventHandler to now what button was clicked
btnView.CommandArgument = ArtID; //Set the commandArguments = to the carID
Panel1.Controls.Add(btnView);
}
}
void btnView_Command(object sender, CommandEventArgs e)
{
string ArtID = e.CommandArgument.ToString(); //Gets theCarId from the CommandArgument
Session["ArtID"] = ArtID; //Create session CarID and set it = to the CarID
Response.Redirect("ViewArt.aspx"); //Redirect to the ViewCar page
}
}

Dynamic C# Not rendering on page_load

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

How to create method for a dynamically added button. asp.net C#

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

Categories