Fail to call event click - c#

I create some Buttons dynamically but when I'm trying to call the event click this doesn't get in, I make some break points to see if something went wrong and doesn't mark an error or something like that...
this is my code where I create and assign some type of data:
/*********************ASSIGN BUTTONS DINAMICALLY***********************/
protected void Assign_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showAndHide();", true);
Button Btn_clic = (Button)sender;
var name = Btn_clic.Text;
List.ListUsers listArea = new List.ListUsers();
List<Data.Area> Area = listArea.AreaList();
List<Data.Area> ListOfEquiposOk = Area.Where(x => x.AREA == name && x.STANDBY == 0).ToList();
List<Button> Botones = new List<Button>();
var TeamFCH = ListOfEquiposOk.Select(x => x.TEAM).Distinct().ToList();
foreach (var team in TeamFCH)
{
Button newButton = new Button();
newButton.CommandName = "Btn" + Convert.ToString(team);
newButton.ID = "Btn_" + Convert.ToString(team);
newButton.Text = team;
newButton.Click += Info_Click;
newButton.CommandArgument = name;
newButton.OnClientClick = "return ModalGood();";
Botones.Add(newButton);
GoodPanel.Controls.Add(newButton);
newButton.CssClass = "btn-primary outline separate";
}
}
/**********************************************************************/
and here is where I try to call the event previously declared:
void Info_Click(object sender, EventArgs e)
{
Button Btnclick = (Button)sender;
var team = Btnclick.Text;
string name = Btnclick.CommandArgument;
List.ListUsers listArea = new List.ListUsers();
List<Data.Area> Area = listArea.AreaList();
List<Data.Area> ListOfToolsOk = Area.Where(x => x.AREA == name && x.TEAM == team && x.STANDBY == 0).ToList();
var ToolArea = ListOfToolsOk.Select(x => x.TEAM);
Grv_Eng.DataSource = ListOfToolsOk;
Grv_Eng.DataBind();
}

Related

c# how to maintain created dynamic buttons at next startup

i am facing a trouble for few days. I had create a dynamic buttons at runtime but it will disappear on the next startup. please help. below is my coding.
private void button1_Click(object sender, EventArgs e)
{
createNewTable createTable = new createNewTable();
DialogResult dResult = createTable.ShowDialog();
string table_name = "";
if (dResult == DialogResult.OK)
{
table_name = createTable.tableName;
createTable.Dispose();
}
else if (dResult == DialogResult.Cancel)
{
createTable.Dispose();
}
if(table_name != string.Empty){
Button textbox = new Button();
textbox.Name = "btn_" + table_name;
textbox.Text = table_name;
textbox.Height = 55;
textbox.Width = 123;
textbox.MouseDown += new MouseEventHandler(textbox_MouseDown);
textbox.MouseMove += new MouseEventHandler(textbox_MouseMove);
textbox.MouseUp += new MouseEventHandler(textbox_MouseUp);
textbox.ContextMenuStrip = contextMenuStrip1;
this.Controls.Add(textbox);
}
}
i had searched some info like using XMLSerialize/application settings but it dont provided any sample for me how to add dynamic button using XMLSerialize. please help..

Click event of dynamically created Button not firing

I'm creating dynamically generated buttons, and when I click on the button, the Add_Click method doesn't get fired up.
Here is a sample from my code:
protected void SearchRec(object sender, EventArgs e)
{
SearchResultsPanel.Controls.Clear();
string text_to_search = SearchTB.Text;
Friends RecToSearch = new Friends();
List<Friends> ListNFU = DBS.getNonFriendUsers(User.Identity.Name.ToString(), text_to_search);
if (ListNFU.Count != 0)
{
foreach (Friends NFRIndex in ListNFU)
{
string _FriendsOutput = FR_output(NFRIndex);
HyperLink RecHyperLink = new HyperLink();
RecHyperLink.Text = _FriendsOutput;
RecHyperLink.CssClass = "HyperLinkFriends";
RecHyperLink.ID = NFRIndex.UdName;
SearchResultsPanel.Controls.Add(new LiteralControl("<div style='height:32px'>"));
SearchResultsPanel.Controls.Add(RecHyperLink);
Button addUser = new Button();
addUser.CssClass = "ApproveBTN";
addUser.Text = "send";
addUser.Click += new EventHandler(Add_Click);
addUser.ID = NFRIndex.UdName + "3";
SearchResultsPanel.Controls.Add(addUser);
}
}
else
{
Label NoResultsLabel = new Label();
NoResultsLabel.Text = "Nothing is found";
SearchResultsPanel.Controls.Add(NoResultsLabel);
}
SearchResultsPanel.Controls.Add(new LiteralControl("</div>"));
}
private void Add_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
string _tempID = btn.ID;
string id = _tempID.Substring(0, _tempID.LastIndexOf('3'));
DateTime cdate = new DateTime();
cdate = DateTime.Now;
DBS.AddFriend(User.Identity.Name, id, cdate);
btn.Visible = false;
btn.NamingContainer.FindControl(id).Visible = false;
}
Note: I did something very similar on page_load and it does work.
That is because when the page is reloaded, the control is most probably not recreated. That means that the event won't fire indeed.
You need to place this kind of code in the Page_Load so it gets recreated at postback.

Creating dynamic controls on Page_LoadComplete

I have three drop down lists and depending on the values selected in the lists I create dynamic controls accordingly. Everything displays properly but when I click on the dynamically created button the events are not firing. I am using the Page_LoadComplete to create the dynamic controls because I need to the values from the DDL's. Is this the reason for my button events not firing?
protected void Page_LoadComplete(object sender, EventArgs e)
{
queries = new clsFormQueries();
if (HttpContext.Current.User.IsInRole("Admin"))
{
if (!Page.IsPostBack)
{
List<Sport> sports = ComboBoxOptions.getSports();
DropDownList temp = (DropDownList)loginView2.FindControl("Sport");
temp.DataSource = sports;
temp.DataTextField = "Name";
temp.DataValueField = "id";
temp.DataBind();
DropDownList teamsDD = (DropDownList)loginView2.FindControl("Team");
List<Team> teamsList = ComboBoxOptions.getTeamsBySportId(Convert.ToInt32(temp.SelectedValue));
teamsDD.DataSource = teamsList;
teamsDD.DataTextField = "fullName";
teamsDD.DataValueField = "teamId";
teamsDD.DataBind();
DropDownList existingPlayers = (DropDownList)loginView2.FindControl("ExistingPlayers");
List<Player> players = ComboBoxOptions.getPlayersBySport(Convert.ToInt32(temp.SelectedValue), Convert.ToInt32(teamsDD.SelectedValue));
existingPlayers.DataSource = players;
existingPlayers.DataTextField = "fullName";
existingPlayers.DataValueField = "playerid";
existingPlayers.DataBind();
//if (existingPlayers.SelectedValue != "" && temp.SelectedValue != "")
//{
// DataTable updates = queries.GetPlayerUpdate(Convert.ToInt32(existingPlayers.SelectedValue), Convert.ToInt32(temp.SelectedValue));
// if (updates.Rows.Count > 0)
// CreateUpdatesHTML(updates);
//}
}
DropDownList temp2 = (DropDownList)loginView2.FindControl("Sport");
DropDownList existingPlayers2 = (DropDownList)loginView2.FindControl("ExistingPlayers");
if (existingPlayers2.SelectedValue != "" && temp2.SelectedValue != "")
{
DataTable updates = queries.GetPlayerUpdate(Convert.ToInt32(existingPlayers2.SelectedValue), Convert.ToInt32(temp2.SelectedValue));
if (updates.Rows.Count > 0)
CreateUpdatesHTML(updates);
}
}
}
private void CreateUpdatesHTML(DataTable updates)
{
foreach (DataRow update in updates.Rows)
{
int playerUpdateId = (int)update["playerUpdateId"];
string updateText = update["PlayerUpdate"].ToString();
TextBox tb = new TextBox();
tb.ID = "UpdateText" + playerUpdateId;
tb.TextMode = TextBoxMode.MultiLine;
tb.Rows = 5;
tb.Text = updateText;
tb.CssClass = "span5";
Button btnDelete = new Button();
btnDelete.Click += new EventHandler(btnDelete_Click);
btnDelete.ID = "Delete"+playerUpdateId.ToString();
btnDelete.Text = "Delete";
btnDelete.CssClass = "btn btn-info";
Button btnUpdate = new Button();
btnUpdate.Click += new EventHandler(btnUpdate_Click);
btnUpdate.ID = "Update"+playerUpdateId.ToString();
btnUpdate.Text = "Update";
btnUpdate.CssClass = "btn btn-info";
HtmlGenericControl div2 = new HtmlGenericControl("div");
div2.Attributes.Add("class", "pull-right span5");
div2.Controls.Add(btnDelete);
div2.Controls.Add(btnUpdate);
HtmlGenericControl hr = new HtmlGenericControl("hr");
HtmlGenericControl br = new HtmlGenericControl("br");
existingUpdates.Controls.Add(tb);
existingUpdates.Controls.Add(div2);
existingUpdates.Controls.Add(br);
existingUpdates.Controls.Add(hr);
}
}
My guess is that since you have wrapped the logic for your Page_LoadComplete event inside of a n if(!Page.IsPostBack), since the click of the button causes a postback, your dynamically created controls are not recreated properly (read: no click event handling is wired up). You need to have logic on every invocation of Page_LoadComplete (non-postback or postback) that recreates the dynamic controls and wires up their events.

GridView RowCommand Events not firing when button created with an external class

Ok The problem is I have a grid that I need to have paging on it with pages shown as numbers
I want to add two link button to the paging section to alow user to navigate to next page prev page
Here is my code
protected void CustomerGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
var grid = sender as GridView
if (e.Row.RowType == DataControlRowType.Pager)
{
var prvLink = new LinkButton();
prvLink.Text = "<";
prvLink.CommandName = "Page";
prvLink.CommandArgument = "Prev";
prvLink.EnableViewState = true;
var nextLink = new LinkButton();
nextLink.Text = ">";
nextLink.CommandName = "Page";
nextLink.CommandArgument = "Next";
nextLink.EnableViewState = true;
var prvCell = new TableCell();
var nextCell = new TableCell();
prvCell.Controls.Add(prvLink);
nextCell.Controls.Add(nextLink);
Table pagerTable = e.Row.Controls[0].Controls[0] as Table;
TableRow row = pagerTable.Rows[0];
row.Cells.AddAt(0, prvCell);
row.Cells.AddAt(row.Cells.Count, nextCell);
if (grid.PageIndex == 0)
{
prvCell.Enabled = false;
}
if (grid.PageIndex == grid.PageCount - 1)
{
nextCell.Enabled = false;
}
}
}
its perfectly working and users are able to navigate back and forward (and I can see grid RowCommand event getting fired)
The problem is I do not want to put the code inside my page (to make my page tiny and put the responsibility to an other class )
here is my class
public class GridStyler
{
private GridView _grid;
public GridStyler(GridView grid)
{
_grid = grid;
}
public void AddNextPreviousOnPager()
{
_grid.RowCreated += _grid_RowCreated;
}
void _grid_RowCreated(object sender, GridViewRowEventArgs e)
{
var grid = sender as GridView;
if (e.Row.RowType == DataControlRowType.Pager)
{
var prvLink = new LinkButton();
prvLink.Text = "<";
prvLink.CommandName = "Page";
prvLink.CommandArgument = "Prev";
prvLink.EnableViewState = true;
var nextLink = new LinkButton();
nextLink.Text = ">";
nextLink.CommandName = "Page";
nextLink.CommandArgument = "Next";
nextLink.EnableViewState = true;
var prvCell = new TableCell();
var nextCell = new TableCell();
prvCell.Controls.Add(prvLink);
nextCell.Controls.Add(nextLink);
Table pagerTable = e.Row.Controls[0].Controls[0] as Table;
TableRow row = pagerTable.Rows[0];
row.Cells.AddAt(0, prvCell);
row.Cells.AddAt(row.Cells.Count, nextCell);
if (grid.PageIndex == 0)
{
prvCell.Enabled = false;
}
if (grid.PageIndex == grid.PageCount - 1)
{
nextCell.Enabled = false;
}
}
}
}
then I should be able to call a code like that in my page load and it should create the link buttons and response to click of them
var g = new GridStyler(CustomerGridView);
g.AddNextPreviousOnPager();
what happens is the link buttons are created just fine but when user clicks them page get refreshed but RowCommand never get fired (they get fired of course when user clicks other buttons but not this two dynamically created buttons)
Any suggestion is really appreciated

Event won't fire in webpart lifecycle

I have a webpart that is quire simple. CLick on "Add" link that sets a 2 text boxes visible. Type in some text and click "Save" button but the click event won't fire. I'm pasting the code it hopes of some suggestion. I've searched for solutions but haven't found anything I can go on. I realize what maight be the issue but don't know how to corrct it. I need to be able to wireup and event with the handler sotimes before the page renders and I tried to override the OnPreRender method but it did not work in right moment.
Other minor issue that I will need to address is that the onFocus method doesn't work in txtMyLinkName.Focus(). Thanks for your help! - Risho
public class MyLinks : WebPart
{
public static string m_Portal = ConfigurationManager.ConnectionStrings["dbPortal"].ConnectionString;
Panel pnlMyLinks = new Panel();
Label lblError = new Label();
Label lblMyLinkURL = new Label();
Label lblMyLinkName = new Label();
TextBox txtMyLinkName = new TextBox();
TextBox txtMyLinkURL = new TextBox();
Button btnSaveMyLink = new Button();
LinkButton lbMyLinkAdd = new LinkButton();
Literal litP1 = new Literal();
Literal litBR1 = new Literal();
public cisf_MyLinks()
{
this.Title = "MyLinks";
this.ExportMode = WebPartExportMode.All;
}
protected override void CreateChildControls()
{
GetLinks();
base.CreateChildControls();
}
//protected override void OnPreRender(EventArgs e)
//{
// btnSaveMyLink.Text = "Save";
// btnSaveMyLink.Click += new EventHandler(btnSaveMyLink_Click);
// Controls.Add(btnSaveMyLink);
// base.OnPreRender(e);
//}
protected void GetLinks()
{
pnlMyLinks.Controls.Clear();
int i = 0;
lbMyLinkAdd.Text = "Add";
pnlMyLinks.Controls.Add(lbMyLinkAdd);
lbMyLinkAdd.Click += new EventHandler(lbMyLinkAdd_Click);
pnlMyLinks.Controls.Add(new LiteralControl("<br />"));
IDataReader drMyLinks = Get_MyLinks(Page.Request.ServerVariables["Logon_User"].Split("\\".ToCharArray())[1].ToLower());
while (drMyLinks.Read())
{
HyperLink hlMyLink = new HyperLink();
LinkButton lbDelMyLink = new LinkButton();
lbDelMyLink.Text = "(del)";
lbDelMyLink.ToolTip = "Delete this link";
lbDelMyLink.CssClass = "verytiny";
lbDelMyLink.Command += new CommandEventHandler(DelMyLink);
lbDelMyLink.CommandName = drMyLinks["id"].ToString();
pnlMyLinks.Controls.Add(lbDelMyLink);
pnlMyLinks.Controls.Add(new LiteralControl(" "));
hlMyLink.ID = "hl" + drMyLinks["ID"].ToString();
hlMyLink.Text = drMyLinks["Title"].ToString();
hlMyLink.NavigateUrl = drMyLinks["url"].ToString();
hlMyLink.Target = "_blank";
hlMyLink.ToolTip = drMyLinks["Title"].ToString();
pnlMyLinks.Controls.Add(hlMyLink);
pnlMyLinks.Controls.Add(new LiteralControl("<br />"));
if (drMyLinks["ID"].ToString() != "") { i += 1; }
}
this.Controls.Add(pnlMyLinks);
}
protected void lbMyLinkAdd_Click(object sender, EventArgs e)
{
lbMyLinkAdd.Visible = false;
lblMyLinkName.Visible = true;
txtMyLinkName.Visible = true;
litBR1.Visible = true;
lblMyLinkURL.Visible = true;
txtMyLinkURL.Visible = true;
btnSaveMyLink.Visible = true;
litP1.Visible = true;
(txtMyLinkName - dot focus)
lblMyLinkName.Text = "Link Name: ";
lblMyLinkURL.Text = "Link URL: ";
btnSaveMyLink.Text = "Save";
btnSaveMyLink.Click += new EventHandler(btnSaveMyLink_Click);
pnlMyLinks.Controls.Add(new LiteralControl("<table class='mylinksTable' cellpadding='0' cellspacing='0' border='1'><tr valign='top'><td>"));
pnlMyLinks.Controls.Add(lblMyLinkName);
pnlMyLinks.Controls.Add(new LiteralControl("</td><td>"));
pnlMyLinks.Controls.Add(txtMyLinkName);
pnlMyLinks.Controls.Add(new LiteralControl("</td></tr><tr valign='top'><td>"));
pnlMyLinks.Controls.Add(lblMyLinkURL);
pnlMyLinks.Controls.Add(new LiteralControl("</td><td>"));
pnlMyLinks.Controls.Add(txtMyLinkURL);
pnlMyLinks.Controls.Add(new LiteralControl("</td></tr><tr valign='top'><td colspan='2'>"));
pnlMyLinks.Controls.Add(btnSaveMyLink);
pnlMyLinks.Controls.Add(new LiteralControl("</td></tr></table>"));
this.Controls.Add(pnlMyLinks);
}
protected void btnSaveMyLink_Click(object sender, EventArgs e)
{
string thisURL;
if ((txtMyLinkName.Text != "") && (txtMyLinkURL.Text != ""))
{
if (txtMyLinkURL.Text.StartsWith("http"))
{ thisURL = txtMyLinkURL.Text; }
else { thisURL = "http://" + txtMyLinkURL.Text; }
AddMyLink(txtMyLinkName.Text, thisURL, Page.Request.ServerVariables["Logon_User"].Split("\\".ToCharArray())[1].ToLower());
GetLinks();
txtMyLinkName.Text = "";
txtMyLinkURL.Text = "";
lbMyLinkAdd.Visible = true;
}
lbMyLinkAdd.Visible = true;
lblMyLinkName.Visible = false;
txtMyLinkName.Visible = false;
litBR1.Visible = false;
lblMyLinkURL.Visible = false;
txtMyLinkURL.Visible = false;
btnSaveMyLink.Visible = false;
litP1.Visible = false;
}
}
If you are creating the button in code, then it needs to be wired up in the Page_Load event so that the click event can fire. Page_PreRender is too late.
In addition to adding the control in Load event as already posted, you should set the ID field e.g. btnSaveMyLink.ID = "SaveLink"; to a unique value.

Categories

Resources