Open modal with button within the table - c#

I have created button in my cs file with the code as below, and try to fire the event handler for the button which inside the table. However, it does not function with it. May I know what should I do?
protected void feesinfo_Click(object sender, EventArgs e)
{
//Hidden field for checking in .aspx file
ClientScript.RegisterHiddenField("typeButton", "FEE_INFO");
System.Diagnostics.Debug.WriteLine("School Fees Details");
Table tbl = new Table();
for (int i = 0; i < 5; i++)
{
TableRow tRow = new TableRow();
Table1.Rows.Add(tRow);
for (int j = 1; j <= 6; j++)
{
TableCell tCell = new TableCell();
if (j == 4)
{
Button btn = new Button();
btn.CssClass = "btn btn-success";
btn.ID = "btndetails";
btn.Text = "400";
tCell.Controls.Add(btn);
btn.Click += new EventHandler(btnClick);
}
else
{
tCell.Text = "Row " + i + ", Cell " + j;
}
tRow.Cells.Add(tCell);
}
}
}
protected void btnClick(object sender, EventArgs e)
{
ClientScript.RegisterHiddenField("typeButton", "Details");
System.Diagnostics.Debug.WriteLine("School Fees Details");
mpe.Show();
}

Related

Buttons in a DataGridView

I am a beginner and I meet a problem with the Buttons in a DataGridView.
When I fill a cell with text that is the row number of the grid, no problem, the text is correct and stay correct if I add a new line.
But if I put this same text on the Button, all the lines are repainted and all the Buttons have the text of the last Button created.
My code is in the click event of a Button out the DataGridView:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
textBox1.Text += "ligne clické " + e.RowIndex+ Environment.NewLine;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
e.RowIndex >= 0)
{
textBox1.AppendText( "bouton clické "+btn.Tag+ Environment.NewLine) ;
}
}
private void button1_Click(object sender, EventArgs e)
{
string[] row;
row = new string[] { "", "Product "+dataGridView1.RowCount };
dataGridView1.Columns[1].ReadOnly = true;
btn.HeaderText = "Click Data";
Random rnd = new Random();
int RND = rnd.Next(1, 1003);
btn.Text = "Click Here"+dataGridView1.RowCount+" " + RND.ToString();
btn.Name = "btn"+RND.ToString();
btn.Tag = "tag ->" + dataGridView1.RowCount;
btn.UseColumnTextForButtonValue = true;
dataGridView1.Rows.Add(row);
}
I found the solution
private void button1_Click(object sender, EventArgs e)
{
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
btn.HeaderText = "Click Data";
Random rnd = new Random();
int RND = rnd.Next(1, 1003);
btn.Text = "Click Here" + dataGridView1.RowCount + " " + RND.ToString();
btn.Name = "btn" + RND.ToString();
btn.Tag = "tag ->" + dataGridView1.RowCount;
int rowIndex = dataGridView1.Rows.Add(btn);
dataGridView1.Rows[rowIndex].Cells[0].Value = btn.Text;
dataGridView1.Rows[rowIndex].Cells[1].Value = "Product " + dataGridView1.RowCount;
}

LinkButton.OnClick Method with Parameter

I'm creating a linkButton Dynamically on ASP.net. I need help on when I click the linkButton, it will store the linkbutton name to my Label1 (for example only).
Here is the code:
protected void Page_Load(object sender, EventArgs e)
{
for (int ctr = 0; ctr <= 2; ctr++)
{
LinkButton link = new LinkButton();
link.ID = "lnk" + ctr.ToString();
link.Text = "lnkName" + ctr.ToString();
link.Click += new EventHandler(DynamicClick);
this.form1.Controls.Add(link);
}
}
//when I click the buttonLink
public void DynamicClick(object sender, EventArgs e)
{
//label1.text will get the .text of the button that i pressed
Label1.Text = "";
}
I would do it in that way
public void DynamicClick(object sender, EventArgs e)
{
LinkButton linkButton = sender as LinkButton;
if (linkButton != null)
{
Label1.Text = linkButton.Text;
}
}
protected void Page_Load(object sender, EventArgs e)
{
for (int ctr = 0; ctr <= 2; ctr++)
{
LinkButton link = new LinkButton();
link.ID = "lnk" + ctr.ToString();
link.Text = "lnkName" + ctr.ToString();
link.Click += delegate { Label1.Text = link.ID; };
this.form1.Controls.Add(link);
}
}
I would do this with a slight variation to Valeh's answer.
protected void Page_Load(object sender, EventArgs e)
{
for (int ctr = 0; ctr <= 2; ctr++)
{
LinkButton link = new LinkButton();
link.ID = "lnk" + ctr.ToString();
link.Text = "lnkName" + ctr.ToString();
link.Click += (s, ea) =>
{
Label1.Text = link.Text;
};
this.form1.Controls.Add(link);
}
}
Creating a separate method public void DynamicClick(object sender, EventArgs e) - especially one that is public - is a bad thing when it is not necessary as it breaks encapsulation. It's perfectly understandable to do so when we're working with a designer, but when we're writing the code ourselves it's a bad idea.

select/deselect button in asp.net by last selected first deselect

I have dynamically created buttons as shown in code all all the button from 10:00 to 12:00 with 15 min of interval and deselect in lifo order like if i have selected 10:0 and 10:15 then i can not select 10:45 .. and i i have selected 10:0,10:15,10:30,10:45 and then i have to deselect 10:15 then first we have to deselect 10:30 and 10:45 ...please help me out.
private void GetControls()
{
count++;
for (int i = 10; i < 12; i++)
{
for (int j = 0; j < 60; j += 15)
{
Button btn = new Button();
btn.Text = i + "-" + j;
btn.ID = i + "-" + j;
btn.Command += new CommandEventHandler(this.btn_Click);
// btn.Click += btn_Click;
flag = true;
btn.CommandName = i + "-" + j;
if (count==1)
{
PlaceHolder1.Controls.Add(btn);
}
}
}
}
private void btn_Click(object sender, CommandEventArgs e)
{
count++;
string ID = (sender as Button).ID;
Label1.Text = " Congrates! Your meeting time has been sheduled upto " + ID;
Label1.Visible = false;
Button btn = sender as Button;
if (btn.BackColor == Color.Green)
{
btn.BackColor = System.Drawing.Color.Yellow;
getStatus(sender);
}
else
{
btn.BackColor = System.Drawing.Color.Green;
}
Correct me if I am wrong! You want to only select one button at a time so that if one button is clicked, the rest should reset? if that's the case, here's a code to do so:
[Edit]
Added a Session to remember dynamic button ids and used (.) instead of (-) for button Ids
private void GetControls()
{
count++;
for (int i = 10; i < 12; i++)
{
for (int j = 0; j < 60; j += 15)
{
Button btn = new Button();
btn.Text = i + "-" + j;
btn.ID = i + "." + j;
btn.Command += new CommandEventHandler(this.btn_Click);
// btn.Click += btn_Click;
flag = true;
btn.CommandName = i + "-" + j;
if (count == 1)
{
PlaceHolder1.Controls.Add(btn);
List<string> createdControls = Session["Controls"] != null ? Session["Controls"] as List<string> : new List<string>();
if (!createdControls.Contains(btn.ID)) createdControls.Add(btn.ID);
Session["Controls"] = createdControls;
}
}
}
}
[Edited] Replace the btn-Click event with the following.
private void btn_Click(object sender, CommandEventArgs e)
{
count++;
string ID = (sender as Button).ID;
ResetButton(Convert.ToDouble(ID));
Label1.Text = " Congrates! Your meeting time has been sheduled upto " + ID;
Button btn = sender as Button;
if (btn.BackColor == Color.Green)
{
btn.BackColor = System.Drawing.Color.Yellow;
}
else
{
btn.BackColor = System.Drawing.Color.Green;
}
}
[Edited] And this is the method implementation
private void ResetButton(double selectedButtonID)
{
List<string> createdControls = Session["Controls"] != null ? Session["Controls"] as List<string> : new List<string>();
TimeSpan timespan = TimeSpan.FromHours(selectedButtonID);
string currentSelectedTime = timespan.ToString("h\\:mm");
foreach (string buttonID in createdControls)
{
if (!string.IsNullOrEmpty(buttonID))
{
int comparisonResult = timespan.CompareTo(TimeSpan.FromHours(Convert.ToDouble(buttonID)));
Button button = Page.FindControl(buttonID) as Button;
if (button != null && comparisonResult==1)
{
button.BackColor = Color.Yellow;// selected
}
else
{
button.BackColor = Color.Red;// deselected
}
}
}

Get text value from dynamically creating text boxes

Given multiple dynamically created textboxes, I want to get the text filled by the user.
I used a Panel and the creation works. The control for the textbox is not found.
ASPX
<div>
<asp:Panel ID="Panel1" runat="server"></asp:Panel>
</div>
<div>
<asp:Button ID="Button1" runat="server" Text="Adauga autor" OnClick="Button1_Click" />
</div>
Create Textboxes
protected void Button1_Click(object sender, EventArgs e)
{
int rowCount = Convert.ToInt32(Session["clicks"]);
rowCount++;
Session["clicks"] = rowCount;
for (int i = 1; i <= rowCount; i++)
{
TextBox TxtBoxA = new TextBox();
Label lblA = new Label();
TxtBoxA.ID = "TextBoxA" + i.ToString();
lblA.ID = "LabelA" + i.ToString();
lblA.Text = "Label" + i.ToString() + ": ";
Panel1.Controls.Add(lblA);
Panel1.Controls.Add(TxtBoxA);
Panel1.Controls.Add(new LiteralControl("<br />"));
}
}
Get Text
int rowCount = Convert.ToInt32(Session["clicks"]);
for (int i = 1; i <= rowCount; i++)
{
string item = "TextBoxA" + i.ToString();
Control foundControl = FindControl(item, Page.Controls);
TextBox TB = (TextBox)foundControl;
string txt = TB.Text;
}
+
public static Control FindControl(string controlId, ControlCollection controls)
{
foreach (Control control in controls)
{
if (control.ID == controlId)
return control;
if (control.HasControls())
{
Control nestedControl = FindControl(controlId, control.Controls);
if (nestedControl != null)
return nestedControl;
}
}
return null;
}
The Textbox Control is null. What am I doing wrong?
You have to recreate all controls in Page_Load at the latest. Button1_Click is too late. So recreate Session["clicks"] in Page_Init/Page_Load and create the single new control in the Button-Click-handler.
Some code:
protected void Page_Init(Object sender, EventArgs e)
{
RecreateControls();
}
private void RecreateControls()
{
int rowCount = Convert.ToInt32(Session["clicks"]);
CreateControls(rowCount);
}
private void AddControl()
{
int rowCount = Convert.ToInt32(Session["clicks"]);
CreateControls(1);
Session["clicks"] = rowCount++;
}
private void CreateControls(int count)
{
for (int i = 1; i <= count; i++)
{
TextBox TxtBoxA = new TextBox();
Label lblA = new Label();
TxtBoxA.ID = "TextBoxA" + i.ToString();
lblA.ID = "LabelA" + i.ToString();
lblA.Text = "Label" + i.ToString() + ": ";
Panel1.Controls.Add(lblA);
Panel1.Controls.Add(TxtBoxA);
Panel1.Controls.Add(new LiteralControl("<br />"));
}
}
protected void Button1_Click(object sender, EventArgs e)
{
AddControl();
}

Add Event at runtime

my method is :
private void button1_Click(object sender, EventArgs e)
{
for (int i = 1; i < 10; i++)
{
Button btn = new Button();
btn.Name = "btn" + i.ToString();
btn.Text = "btn" + i.ToString();
btn.Click += new EventHandler(this.btn_Click);
this.flowLayoutPanel1.Controls.Add(btn);
}
}
void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn.Name == "btn1")
{
this.Text = "stack";
}
}
There is a better approach ?
The code you used:
btn.Click += new EventHandler(this.btn_Click);
Is the correct code to add the handler. Creating the buttons and adding them to their container looks good.
The only thing I would add is just make sure you are creating the controls on postback too, prior to viewstate being restored, so that the events can actually be called.
Or maybe:
private void button1_Click(object sender, EventArgs e)
{
for (int i = 1; i < 10; i++)
{
Button btn = new Button();
btn.Text = "btn" + i.ToString();
btn.Tag = i;
btn.Click += delegate
{
if ((int)btn.Tag == 1)
this.Text = "stack";
};
this.flowLayoutPanel1.Controls.Add(btn);
}
}

Categories