In my WebForm I want the total price changed when the user check one product but I have this problem with my code ,
the CheckedChanged event is not firing when I check the CheckBox
It is firing only when I click the Button(used for as clear button) , and I didn't include that code within the button event !
Here is my code:
public partial class _Default : System.Web.UI.Page
{
int total = 0;
String strtotal;
protected void ckb1_CheckedChanged(object sender, EventArgs e)
{
if (ckb1.Checked)
{
total = total + 100;
strtotal = total.ToString();
lbl2.Text = strtotal;
}
}
protected void ckb2_CheckedChanged(object sender, EventArgs e)
{
if (ckb2.Checked)
{
total = total + 80;
strtotal = total.ToString();
lbl2.Text = strtotal;
}
}
protected void ckb3_CheckedChanged(object sender, EventArgs e)
{
if (ckb3.Checked)
{
total = total + 70;
strtotal = total.ToString();
lbl2.Text = strtotal;
}
}
protected void Button3_Click(object sender, EventArgs e)
{
TextBox1.Text = " ";
ckb1.Checked = false;
ckb2.Checked = false;
ckb3.Checked = false;
}
}
All ASP.NET Server controls except Button, Hyperlink and LinkButton have a default AutoPostBack property of false, So you should set AutoPostBack="true" in your CheckBox:
<asp:CheckBox ID="ckb1" runat="server" AutoPostBack="true" OnCheckedChanged="ckb1_CheckedChanged" />
It is firing only when I click the button
As I said this is because the Button have AutoPostBack property of true by default so after you checked the CheckBox and then click the button the CheckBox state automatically posts back to the server.
Related
I'm creating dynamically buttons into a repeater like somebody recommend in previous questions I make into a Page_Init and I make some conditions but the problem persist, the button don't fire the assigned event
this is the first condition
public bool AssignClicked
{
get
{
return Convert.ToBoolean(ViewState["AssignClicked"]);
}
set
{
ViewState["AssignClicked"] = value;
}
}
then this is the code when I make click in the button that create the buttons changing into true the condition:
protected void DButton(object sender, EventArgs e)
{
AssignClicked = true;
Page_Init(sender, e);
Page_Load(sender, e);
}
And start the creations of the buttons
protected void Page_Init(object sender, EventArgs e)
{
if (AssignClicked)
{
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();
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.CommandArgument = name;
newButton.Click += new EventHandler(newButton_Click);
Repeater1.Controls.Add(newButton);
newButton.CssClass = "btn-primary outline separate";
}
}
}
but when I click over the button created this doesn't do anything
I have a column in SQL:
Status
open
Close
and Gridview with Boundfield value='Status'
When a user selects a row and the Status == open then it should display a button. Otherwise ist hiden.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string y = Data.Rows[GridView1.SelectedIndex][5].ToString();
if (y == "open")
{
btnAccept.Visible = true;
}
else
{
btnAccept.Visible = false;
}
}
<asp:Button Text="Accept" OnClick="btnAccept_Click" Visible="false" ID="btnAccept" runat="server" />
This could be the solution:
string y = GriView1.SelecteRows[0].Cells[5].Value.ToString();
Modify your code like this
string y = GridView1.SelectedRow.Cells[5].Text;
It's counter-intuitive, but GridView1.SelectedRow is not set until after SelectedIndexChanged completes.
Also visibility has nothing to do with the button being Enabled or Disabled.
Use the Enabled Property of the Button:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView gv = (GridView) sender ;
btnAccept.Enabled = (gv.Rows[ gv.SelectedIndex ].Cells[5].Text == "open");
}
Also, if you haven't already, consider styling disabled buttons with CSS:
input[type=button][disabled],
button[disabled]
{
cursor:not-allowed;
}
I am doing a custom Pager Template for a gridview & trying to get something like:
< 1 2 3 4 >
I am having problems clicking on the numbers...
Inside the PagerTemplate i have a Placeholder named "phLinks" that is being filled on the DataBound Event like this:
protected void DataBound(object sender, EventArgs e)
{
if (grid.PageCount > 1)
{
for (int i = 0; i < grid.PageCount; i++)
{
phLinks.Controls.Add(new LiteralControl("<li>"));
LinkButton lb = new LinkButton { Text = (i + 1).ToString(), Enabled = grid.PageIndex != i, CommandArgument = i.ToString() };
lb.Command += new CommandEventHandler(lb_Command);
lb.CommandName = "Button";
//lb.Click += new EventHandler(lb_Click);
phLinks.Controls.Add(lb);
phLinks.Controls.Add(new LiteralControl("</li>"));
}
}
}
Please note that I have tried first the Click Event and then the Command Event, in both cases i get a post back but the event is not triggered...
What am i missing here?
Either bind your GridView on every postback :
protected void Page_Load(object sender, EventArgs e)
{
MyGridView.DataBind();
}
OR turn off ViewState for your GridView::
<asp:GridView ID="MyGridView" EnableViewState="false" ... />.
I have created a button whereby when it is clicked, it should display 3 textboxes in a row. However, every time i click the submit button, the textbox would display 2 rows(which is 6 textboxes). And subsequently increase 2 rows at every click.
Is there anyway that it could just increase one row of textbox at every click??
Here is the code infront:
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</div>
</form>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
// Add any controls that have been previously added dynamically
for (int i = 0; i < TotalNumberAdded; i++)
{
AddControls(i + 1);
}
// Attach the event handler to the button
Button1.Click += new EventHandler(Button1_Click);
}
protected void Button1_Click(object sender, EventArgs e)
{
// Increase the number added and add the new label and textbox
TotalNumberAdded++;
AddControls(TotalNumberAdded);
}
private void AddControls(int controlNumber)
{
TextBox TxtBoxU = new TextBox();
TextBox TxtBoxE = new TextBox();
TextBox TxtBoxY = new TextBox();
Label lblU = new Label();
Label lblE = new Label();
Label lblY = new Label();
TxtBoxU.ID = "TextBoxU" + controlNumber;
TxtBoxE.ID = "TextBoxE" + controlNumber;
TxtBoxY.ID = "TextBoxY" + controlNumber;
lblU.ID = "LabelU" + controlNumber;
lblE.ID = "LabelE" + controlNumber;
lblY.ID = "LabelY" + +controlNumber;
lblU.Text = "User : ";
lblE.Text = "E-Mail : ";
lblY.Text = "Phone number : ";
//Add the labels and textboxes to the Panel.
Panel1.Controls.Add(lblU);
Panel1.Controls.Add(TxtBoxU);
Panel1.Controls.Add(TxtBoxU);
Panel1.Controls.Add(lblE);
Panel1.Controls.Add(TxtBoxE);
Panel1.Controls.Add(lblY);
Panel1.Controls.Add(TxtBoxY);
Panel1.Controls.Add(new LiteralControl("<br>"));
}
protected int TotalNumberAdded
{
get { return (int)(ViewState["TotalNumberAdded"] ?? 0); }
set { ViewState["TotalNumberAdded"] = value; }
}
Your problem is in your page load, becouse your click event declaretion is redundant, you are attaching the EventHandler when you are assinging by HTML, And If you push click the event, It is fires 2 time.
Remove
Button1.Click += new EventHandler(Button1_Click);
And you page load will looks like:
protected void Page_Load(object sender, EventArgs e)
{
// Add any controls that have been previously added dynamically
for (int i = 0; i < TotalNumberAdded; i++)
{
AddControls(i + 1);
}
}
I hope that help.
P.S: I'm sorry in my first test I did not test right.
protected void Button1_Click(object sender, EventArgs e)
{
TextBox txtobj = new TextBox();
Form.Controls.Add(txtobj);
txtobj.Text = "Dynamically Textbox";
}
How can I get the name of the object last clicked on a panel? The trick is there is a big array of buttons on the panel (btn[1] ... btn [200]). How can I check if I clicked on button b[180], or b[11] or even outside the panel (no button)? Also the buttons are generated at page load via coding.
Thank you. Anna
EDIT:
Thank you! Another issue that arose (this generated a NULL object reference):
I have a method on the same level as buttonHandler(), it is named HowManyClicked() and it's called from within buttonHandler(). Inside HowManyClicked() I want to identify Button btn1 = Panel2.FindControl(x) as Button; where x is, for example, buttonArray[2,3]. But I always get NULL. Is the button array buttonArray not identifiable by name once out of the method that generated it??
public void buttonHandler(object sender, EventArgs e)
{
Button btn = sender as Button;
//string tt = btn.ToolTip.ToString();
btn.BackColor = Color.Red;
statusL.Text = HowManyClicked().ToString();
}
public int HowManyClicked()
{
int sum=0;
for (int a = 0; a < 10; a++)
for (int b = 0; b < 14; b++)
{
string x = "buttonArray[" + a + ", " + b + "]";
statusL.Text = x;
Button btn1 = Panel2.FindControl(x) as Button;
if (btn1.BackColor == Color.Red) sum += 1;
}
return sum;
}
As #AVD commented you can get the button originating the postback casting the sender object, you can also use the CommandName and CommandArgument properties from the button object (they are usually used when the button is inside a Grid, DataList etc but you can use them in this context if you need):
protected void Page_Init(object sender, EventArgs e)
{
var s = Enumerable.Range(1, 10);
foreach (var item in s)
{
var b = new Button();
b.Text = "My Button " + item.ToString();
b.CommandName = "custom command";
b.CommandArgument = item.ToString();
b.Click += new EventHandler(b_Click);
this.myPanel.Controls.Add(b);
}
}
void b_Click(object sender, EventArgs e)
{
var current = sender as Button;
this.lblMessage2.Text = "Clicked from array buttons: <br/>Command Name: " + current.CommandName + "<br/>Args: " + current.CommandArgument + "<br/>Button Unique ID: " + current.UniqueID + "<br/>Client ID: " + current.ClientID;
}
Page:
<asp:Panel runat="server" ID="myPanel">
</asp:Panel>
<asp:Label ID="lblMessage2" runat="server" />
This code generates something like:
As an additional note, Microsoft recommends to create dynamic controls in the PreInit event or in case you are using a master page, in the Init event
source
Edited
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.ViewState["count"] = 0;
}
}
protected void Page_Init(object sender, EventArgs e)
{
var s = Enumerable.Range(1, 10);
foreach (var item in s)
{
var b = new Button();
b.Text = "My Button " + item.ToString();
b.Click += new EventHandler(buttonHandler);
this.myPanel.Controls.Add(b);
}
}
void buttonHandler(object sender, EventArgs e)
{
// update here your control
var b = sender as Button;
b.BackColor = Color.Red;
HowManyClicked();
}
private void HowManyClicked()
{
var c = (int)this.ViewState["count"];
c++;
this.ViewState["count"] = c;
this.lblMessage2.Text = "Clicked controls: " + this.ViewState["count"].ToString();
}
This produced:
How can I get the name of the object last clicked on a panel?
The first parameter of click handler returns the reference of control/object has raised the event.
public void buttonHandler(object sender, EventArgs e)
{
Button btn=sender as Button;
....
}
I just figured out another fix by just redefining HowManyClicked() so I am adding it here below. Not sure still why the first method (the one in my question) didn't work also. Here goes:
public int HowManyClicked()
{
int sum=0;
foreach (Control cnt in this.Panel2.Controls)
if (cnt is Button)
{
Button btn = (Button)cnt;
if (btn.BackColor == Color.Red)
sum += 1;
}
return sum;
}
}
Thanks everyone!