On textbox OnTextChanged event the postback cycle triggering twice. Breakpoints on both methods to understand the issue.
Here is my code sample
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="TextBox1" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true" />
</div>
<asp:Label ID="Label1" runat="server"></asp:Label>
</form>
Its code behind.
public static int cycle { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
cycle++;
Label1.Text = cycle.ToString();
}
Avoid using AUTOPOSTBACK, keep the OnTextChanged event trap
and add a button (hidden or not) to catch the return pression on the textbox
to produce the POSTBACK.
Here is an example
<asp:Panel runat="server" CssClass="col-md-2">
<asp:Panel runat="server" CssClass="form-group input-group" DefaultButton="BTN_Cerca">
<span class="input-group-btn">
<asp:Button runat="server" ID="BTN_Cerca" Text="Codice" CssClass="btn btn-secondary" ToolTip="Cerca in magazzino"/>
</span>
<asp:TextBox runat="server" ID="TXT_Search" CssClass="form-control" placeholder="Numero Articolo" OnTextChanged="TXT_Search_TextChanged" />
</asp:Panel>
</asp:Panel>
Related
<asp:UpdatePanel ID="showsDatalistPanel" runat="server" Visible="false" UpdateMode="Always">
<ContentTemplate>
<div>
<asp:DropDownList ID="dateTimeFilter" CssClass="dateTimeFilterIdentifierCls" runat="server"
onclick="dateTimeFilter_SelectedIndexChanged" AutoPostBack="true" Visible="true" ClientIDMode="Static">
</asp:DropDownList>
<label id="dateTimeFilterLabel" runat="server" style="padding-left: 15px" visible="false">
בחירת מופע לפי תאריך
</label>
</div>
how can I call server side function on clicking the dropdown?
Thanks
If you really want to use click event then you have to simulate it using JS as DropDownList doesn't have build-in onclick functionality:
<asp:DropDownList ID="dateTimeFilter" CssClass="dateTimeFilterIdentifierCls" runat="server"
onclick="__doPostBack('dateTimeFilter', null);" AutoPostBack="true" Visible="true" ClientIDMode="Static">
</asp:DropDownList>
and then in your server code you have modify page load event accordingly:
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
if(Request.Params.Get("__EVENTTARGET").ToString() == dateTimeFilter.ClientID)
{
dateTimeFilter_SelectedIndexChanged(dateTimeFilter, null);
}
}
}
But I'm sill confused why you can't just use standard OnSelectedIndexChanged or OnSelectedValueChanged?
I am creating a listview and binding data from database.
<asp:ListView runat="server" ID="lstService" DataTextField="Name"
DataValueField="Id" AutoPostBack="True">
<LayoutTemplate>
<table runat="server" id="table1">
<tr runat="server" id="itemPlaceholder"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td runat="server">
<asp:Label ID="NameLabel" runat="server"
Text='<%#Eval("Name") %>' Width="500px" Height="30px" />
<asp:Button CssClass="btn btn-default" ID="srvButton" runat="server"
Text="Add" OnClick="srvButton_Click" />
<div class="voffset3"></div>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
On code behind I am binding data on this field. I am fetching ID, Name column.
Now it is showing label and button with text "Add" on the button. But when I click on Add , I want to pass the value of "Id" associated with that list view item, how can I do that ?
protected void srvButton_Click(object sender, EventArgs e)
{
}
First add the CommandArgument to your Button with the value of the item Id:
<asp:Button CssClass="btn btn-default" ID="srvButton" runat="server" Text="Add" OnClick="srvButton_Click" CommandArgument='<%#Eval("Id")' />
Now you can access it in the srvButton_Click handler:
protected void srvButton_Click(object sender, EventArgs e)
{
Button myButton = (Button)sender;
int i = Convert.ToInt32(myButton.CommandArgument.ToString());
//----
}
You can change the OnClick to an OnCommand and send a CommandArgument along with it.
<asp:Button CssClass="btn btn-default" ID="srvButton" runat="server"
Text="Add" OnCommand="srvButton_Command" CommandArgument=<%# Eval("ID") %> />
And then in code behind
protected void srvButton_Command(object sender, CommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandArgument);
}
Or you can use OnItemCommand event of the ListView along with the DataKeyNames property and use a button without an event bound to it.
<asp:ListView runat="server" ID="lstService" DataKeyNames="id"
OnItemCommand="lstService_ItemCommand">
<asp:Button CssClass="btn btn-default" ID="srvButton" runat="server" Text="Add" />
And then in code behind get the datakey from the ListView.
protected void lstService_ItemCommand(object sender, ListViewCommandEventArgs e)
{
int id = Convert.ToInt32(lstService.DataKeys[e.Item.DataItemIndex].Values[0]);
}
I have the following code in my .aspx file:
<asp:TextBox ID="txtSearch" runat="server" Width="278px"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" AutoPostBack="true" />
When I click on the btnSearch button it does an AutoPostBack.
What my goal is that if btnSearch was clicked, I would then capture the value of txtSearch or else I would not
How do I code such that if btnSearch was clicked on AutoPost I can flag it.
First, a Button does not have an AutoPostBack property. It posts back always.
Second, you can simply handle it's Click event and read the txtSearch.Text property:
<asp:TextBox ID="txtSearch" runat="server" Width="278px"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
codebehind:
protected void btnSearch_Click(Object sender, EventArgs e)
{
string search = txtSearch.Text;
// ...
}
I want when I click button my textbox1 must change, but textbox3 not
Why does not it work?
protected void Page_Load(object sender, EventArgs e)
{
TextBox3.Text = DateTime.Now.ToLongTimeString();
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = DateTime.Now.ToLongTimeString();
}
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button"/>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ContentTemplate>
<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</asp:UpdatePanel>
When I Click Button1 my TextBox3 changes
Why?
Because when you click the button you submit a request to the server, which results in a round-trip, which results in Page_Load being executed again. You can avoid this by detecting whether the request is part of the postback cycle using the IsPostBack property:
if (IsPostBack) {
}
Or, as is in most cases, doing stuff when it's not a postback:
if (!IsPostBack) {
TextBox3.Text = DateTime.Now.ToLongTimeString();
}
I've tried putting an asp button (with use submit equals false) or an asp textbox that should open the popup pnel (which I've defined), but the popupextender causes a postback and displays all of the current page content (instead of iring the page method I defined).
It's nearly 5 hours of debugging, opening new projects (to try and reproduct the case in other project) and so on.
Thanks in advance...
This is the body of the page that handles the user control:
<body>
<form id="form1" runat="server">
<div>
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></ajaxToolkit:ToolkitScriptManager>
<ucpop:popup ID="gv" runat="server" />
</div>
</form>
This is the usercontrol:
<asp:UpdatePanel runat="server" ID="upExample">
<ContentTemplate>
<asp:GridView runat="server" ID="gvCars"
OnDataBinding="gvCars_DataBinding"
OnRowCommand="gvCars_RowCommand"
>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="ibPopup" ImageUrl="~/Resources/Perspective-Button-Go-icon.png" Width="20px" Height="20px" />
<ajaxToolkit:PopupControlExtender ID="pce" runat="server" TargetControlID="ibPopup" DynamicControlID="pToUpdate"
PopupControlID="pToExtend" DynamicContextKey='<%#Eval("id") %>' DynamicServiceMethod="GetDynamicContent">
</ajaxToolkit:PopupControlExtender>
<asp:Panel runat="server" ID="pToExtend" BackColor="Red" style="display:none">Hello
<asp:Panel runat="server" ID="pToUpdate">
</asp:Panel>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lidPopup" Text='<%=Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lNamePopup" Text='<%=Eval("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton CommandName="p" runat="server" ID="ibNotPopUp" ImageUrl="~/Resources/Perspective-Button-Go-icon.png" Width="20px" Height="20px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
This is the Page web codebehind:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string GetDynamicContent(string contextKey)
{
StringBuilder sTemp = new StringBuilder();
sTemp.Append(string.Format("<div>Cool! I'm {0}</div>", contextKey));
return sTemp.ToString();
}
}
This is the UserControl codebehind:
public partial class GridViewWithPopUpControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvCars.DataBind();
}
}
protected void gvCars_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
string t = e.CommandName;
}
}
protected void gvCars_DataBinding(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
gv.DataSource = CarList.CarCollection;
}
}
It's pretty standard code... yet not working as expected. I think of upgrading the ajaxtoolkit, but other features in the web site system might be ruined, so I have to think it over.
Thanks again.
Supposedly, the Page method wasn't in the page but rather in the User Control.
I did not write the User Control nor the page, I just needed to add functionality and found that problem - and fixed it...