I have simple page Page.aspx like this:
<asp:Content ID="Content2" runat="server">
<asp:PlaceHolder ID="phNewAddress" runat="server" />
</asp:Content>
In code behind is:
protected override void Page_Load(object sender, EventArgs e)
{
AddressDetail xControl = (AddressDetail)Page.LoadControl("AddressDetail.ascx");
phNewAddress.Controls.Add(xControl);
}
AddressDetail.ascx is:
<asp:TextBox ID="address_name" runat="server" />
<asp:RequiredFieldValidator ID="rfv" runat="server"
ControlToValidate="address_name" ErorMessage="Required" ValidationGroup="save">*
</asp:RequiredFieldValidator>
<asp:DropDownList ID="address_state" runat="server" AutoPostBack="true" />
<asp:Button ID="btnSave" runat="server" Text="Save" ValidationGroup="save" OnClick="btnSave_Click" />
DropDownList address_state is binding from datasource.
Now. After page is loaded and I press Save button, validator doesn't work.
But if I change DropDownList, so fire postback, and then press Save button, validator works fine.
Can anyone help me?
I am guessing validation controls work with viewstate, Move the below code from Page_load event to Page_Init event and see if it works.
protected override void Page_Init(object sender, EventArgs e)
{
AddressDetail xControl = (AddressDetail)Page.LoadControl("AddressDetail.ascx");
phNewAddress.Controls.Add(xControl);
}
Related
I am generating buttons inside foreach loop
<% foreach (var myObject in myObjectList)
{
%>
<b>Text field</b>: <%= myObject.Text%><br>
<asp:Button ID="" runat="server" OnClick="WaitQueueDeleteBtn_Click" CommandArgument="pass myObjectText" Text="Delete"/>
<% } %>
It seems that I cannot use myObject's field inside CommandArgument however myObjectList is accessible
i.e
<asp:Button ID="" runat="server" OnClick="WaitQueueDeleteBtn_Click" CommandArgument="<%#myObjectList.Count%>" Text="Delete"/>
The above statement would work but the one below would not
<asp:Button ID="" runat="server" OnClick="WaitQueueDeleteBtn_Click" CommandArgument="<%#myObject.Text %>" Text="Delete"/>
Any idea why? And how can I pass myObject's field values as CommandArgument?
Update:
Button method in class
protected void WaitQueueDeleteBtn_Click(object sender, EventArgs e)
{
}
It's so easy with Repeater
Markup
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<div><b>Text field</b>: <%# Eval("Text") %></div>
<asp:Button runat="server" CommandArgument='<%# Eval("ID") %>' Text="Delete" />
</ItemTemplate>
</asp:Repeater>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Repeater1.DataSource = YOUR_DATA_SOURCE; // myObjectList
Repeater1.DataBind();
// ...
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandArgument == null) return;
var id = int.Parse(e.CommandArgument.ToString());
// your logic here ...
}
You can add multiple buttons and using CommandName can figure out which one clicked.
Hope this helps.
I have 2 buttons in my webpage.When i press "Enter",it auto perform "close_topic",but i want my enter button to perform "button_click".
<asp:Button ID="btnLogout" Text="Close Topic" OnClick="close_topic" runat="server" />
<asp:Button ID="ButtonTT" Text="Click" runat="server" OnClick="button_click" />
protected void button_Click(object sender, EventArgs e)
{
}
protected void close_topic(object sender, EventArgs e)
{
}
tried to use <asp:Panel ID="Panel1" runat="server" DefaultButton="ButtonTT">,but not working.
Did you wrap your panel around the 2 buttons? As follows:
<asp:Panel ID="Panel1" runat="server" DefaultButton="ButtonTT">
<asp:Button ID="btnLogout" Text="Close Topic" OnClick="close_topic" runat="server" />
<asp:Button ID="ButtonTT" Text="Click" runat="server" OnClick="button_click" />
</asp:Panel>
Edit
Based on how to set a default 'enter' on a certain button, you can do it using code also:
Me.Form.DefaultButton = Me.btn.UniqueID;
or
Me.Page.Form.DefaultButton = = Me.btn.UniqueID;
Replacing the Me.Page with whatever your page name is.
I have an ASP.NET DataList, with a footer defined thus:
<FooterTemplate>
<asp:DropDownList ID="ddlStatusList" runat="server">
</asp:DropDownList>
<input id="txtNotes" type="text" placeholder="Notes" />
<asp:Button runat="server" type="button" Text="Add" ID="btnAdd"></asp:Button>
</FooterTemplate>
What I'm looking to do, is, on click of btnAdd, get the values from txtNotes and ddlStatusList, but I can't work out how to access the controls, let alone the values.
I can't follow something like this because I won't be able to check if my button has been clicked (as is possible with a checkbox), and even then, I'm not sure if I'll be able to use findControl as demonstrated. (Does a DataList's footer behave differently to an item?)
I can't use the Button's commandName & commandValue attributes because, when databound, the text inputted won't exist and therefore I can't set the CommandValue.
I have tried using a LinkButton rather than a normal .NET Button, but come accross the same issue, whereby I cannot work out how to get the values from the TextBox/DropDownList
Following should work. See I added runat="Server" for txtNotes:
aspx:
<FooterTemplate>
<asp:DropDownList ID="ddlStatusList" runat="server">
</asp:DropDownList>
<input id="txtNotes" runat="server" type="text" placeholder="Notes" />
<asp:Button runat="server" type="button" Text="Add" ID="btnAdd"></asp:Button>
</FooterTemplate>
C#:
protected void btnAdd_Click(object sender, EventArgs e)
{
var txtNotes = (System.Web.UI.HtmlControls.HtmlInputText)(((Button)sender).Parent).FindControl("txtNotes");
var ddlStatusList = (DropDownList)(((Button)sender).Parent).FindControl("ddlStatusList");
}
You can use Control.NamingContainer to access other controls in a row:
<FooterTemplate>
<asp:DropDownList ID="ddlStatusList" runat="server">
</asp:DropDownList>
<input id="txtNotes" type="text" placeholder="Notes" runat="server" />
<asp:Button runat="server" type="button" Text="Add" ID="btnAdd" OnClick="btnAdd_Click"></asp:Button>
</FooterTemplate>
protected void btnAdd_Click(object sender, EventArgs e)
{
Button btnAdd = (Button)sender;
DropDownList ddlStatusList = (DropDownList)btnAdd.NamingContainer.FindControl("ddlStatusList");
System.Web.UI.HtmlControls.HtmlInputText txtNotes = (System.Web.UI.HtmlControls.HtmlInputText)btnAdd.NamingContainer.FindControl("txtNotes");
int index = ddlStatusList.SelectedIndex;
string text = txtNotes.Value;
}
Using VS2012, I have a button that gets clicked to return some info to a textbox on the screen. I also have a label, label3, that is set to the following
protected void Page_Load(object sender, EventArgs e)
{
Label3.Text = ("THE FIRST RUN RECORDED AT" + DateTime.Now.ToString(" hh:mm:ss tt"));
}
Now naturally every time I press the button to return my information in the other part of the c#/web form it refreshes the hh:mm:ss to the current time.
My question is - Where I can call my function to stop my label from refreshing when the button is clicked?
The button calls this
protected void Button1_Click(object sender, EventArgs e)
{
WebService1 ws1 = new WebService1();
Label2.Text = ws1.codes(TextBox1.Text);
}
It just returns a string dependent on input.
Place Label3 out of UpdatePanel and other controls inside UpdatePanel.
In aspx :
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Label ID="Label3" runat="server"></asp:Label>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click2" Text="OK" />
<br />
<asp:Label ID="Label2" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
I have some controls within an UpdatePanel. Included are some buttons. How can I make these buttons so that they do a full postback rather than a partial AJAX postback?
Use a PostbackTrigger rather than an AsyncPostbackTrigger
Here's an example demonstrating how to use the PostbackTrigger instead of the AsyncPostbackTrigger:
ASPX Page:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="MyLabel" runat="server" />
<br/>
<asp:button ID="AjaxPostbackButton" Text="AJAX Postback" OnClick="AjaxPostbackButton_Click" runat="server" />
<asp:button ID="FullPostbackButton" Text="Full Postback" OnClick="FullPostbackButton_Click" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="AjaxPostbackButton" />
<asp:PostBackTrigger ControlID="FullPostbackButton" />
</Triggers>
</asp:UpdatePanel>
Code Behind:
private void AjaxPostbackButton_Click(object sender, EventArgs e)
{
MyLabel.Text = "Ajax Postback: " + DateTime.Now;
}
private void FullPostbackButton_Click(object sender, EventArgs e)
{
MyLabel.Text = "Full Postback: " + DateTime.Now;
}
Clicking on the "AJAX Postback" button will will update the panel using AJAX whereas the "Full Postback" button will reload the entire page.