How to set focus on first image in my Panel? - c#

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

Related

ImageButton not firing on the second time

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

Cannot retrieve value of dynamically created textboxes

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??

Auto generated Gridview - Change columns width

i'm in a trouble!
I'm generating a lot of gridviews dinamically, with dinamic values to columns.
The columns are ever the same.
I want to know how can i set the size of this columns.
That's my code, with my effort.
private void generateControls( List<List<DataRow>> grids)
{
DataTable dt = new DataTable();
int i = 0;
foreach (List<DataRow> lst in grids)
{
dt = lst.CopyToDataTable();
GridView grv = new GridView();
grv.AlternatingRowStyle.BackColor = System.Drawing.Color.FromName("#cccccc");
grv.HeaderStyle.BackColor = System.Drawing.Color.Gray;
grv.Width = new Unit("100%");
//grv.RowStyle.Wrap = false;
grv.RowStyle.Width = new Unit("100%");
grv.ID = "grid_view" + i;
grv.DataSource = dt;
grv.DataBind();
/* grv.Columns[0].ItemStyle.Width = new Unit("5%");
grv.Columns[1].ItemStyle.Width = new Unit("7%");
grv.Columns[2].ItemStyle.Width = new Unit("12%");
grv.Columns[3].ItemStyle.Width = new Unit("12%");
grv.Columns[4].ItemStyle.Width = new Unit("7%");
grv.Columns[5].ItemStyle.Width = new Unit("7%");
grv.Columns[6].ItemStyle.Width = new Unit("23%");
grv.Columns[7].ItemStyle.Width = new Unit("22%");
grv.Columns[8].ItemStyle.Width = new Unit("5%");*/
Label lblBlankLines = new Label();
lblBlankLines.Text = "<br />";
Panel panelGrid = new Panel();
panelGrid.ID = "panel_grid" + i;
Label lblTipo = new Label();
string tipoOcorrencia = lst[0]["Ocorrência"].ToString();
/*
* Capitalized
* TextInfo myTI = new CultureInfo("pt-BR", false).TextInfo;
string novoTipoOcorrencia = myTI.ToTitleCase(tipoOcorrencia);*/
int quantidade = lst.Count;
lblTipo.Text = " - " + tipoOcorrencia + ": " + quantidade;
LinkButton lkBtn = new LinkButton();
lkBtn.ID = "link_button" + i;
lkBtn.Text = "Exibir | Ocultar";
lkBtn.Attributes["onClick"] = "javascript:return ocultaGrid('" + panelGrid.ID + "'), false";
panel_status.Controls.Add(lblBlankLines);
panel_status.Controls.Add(lkBtn);
panel_status.Controls.Add(lblTipo);
panelGrid.Controls.Add(grv);
panel_status.Controls.Add(panelGrid);
panel_status.DataBind();
i++;
}
}
I've tried to get the columns, but i got an error, telling me an invalid index access.
How can i access my columns in that gridview?
Use the RowDataBound EventHandler:
Count your indexes and make sure you are not trying to access a non-existent column as well.
See link for an example:
http://msdn.microsoft.com/en-us/library/ms178296(v=vs.100).ASPX
grv.RowDataBound += grv_RowDataBound;
private void grv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Width = new Unit("5%");
e.Row.Cells[1].Width = new Unit("7%");
e.Row.Cells[2].Width = new Unit("12%");
e.Row.Cells[3].Width = new Unit("12%");
e.Row.Cells[4].Width = new Unit("7%");
e.Row.Cells[5].Width = new Unit("7%");
e.Row.Cells[6].Width = new Unit("23%");
e.Row.Cells[7].Width = new Unit("22%");
e.Row.Cells[8].Width = new Unit("5%");
}
}

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

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