Incomprehension in asp.net - asp.net

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();
}

Related

Response.Redirect not working when using UpdateProgress

I use an UpdatePanel and UpdateProgress.
Both Response.Redirect and JavaScript redirect window not working with them.
This code is working:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdateProgress ID="UpdateProgress1" runat="server" DynamicLayout="true"
AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<asp:Image ID="ImageWait" runat="server" ImageUrl="../images/wait.gif" />
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="ButtonSave" runat="server" Text="Save"
OnClick="ButtonSave_Click" ValidationGroup="Valid1" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonSave"
EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Not working :
protected void ButtonSave_Click(object sender, EventArgs e)
{
// Saved here
Response.Redirect("../Main/Success.aspx");
}
Not working, too :
protected void ButtonSave_Click(object sender, EventArgs e)
{
// Saved here
Page.ClientScript.RegisterStartupScript(this.GetType(), "redirect",
"location.href = '../Main/Success.aspx'", true);
}
How can I fix this ?
You can use the ScriptManager
protected void redirector_Click(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(this, GetType(), "redirect",
"location.href = 'ListViewDemo.aspx';", true);
}

Textbox asp.net postback trigger twice on autopostback true

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>

Asp.Net Update Panel Required Field Validator Issue

I have an update panel, and inside the update panel there is a dropdown and a required field validator which is associated with the dropdown and the autopostback of dropdown set to true.
now whenever user change the option the autopost back occure in updatepanel.. what i need is that if user pick the empty item from the dropdown it should not postback to the server and validator should fire.... but in my case the validators fire perfecty and also there is a postback and after the postback the required field validaor state is true.
what i need is to stop the postback if validator fails.
i have search for this problem alot but doe's not find any thing helpful.
1- In DropDownList:
CausesValidation="True"
2- In Page_Load use string.Empty to value of first item:
DropDownList1.Items.Add(new ListItem("Select...", string.Empty));
ASPX:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
CausesValidation="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="DropDownList1" Display="Dynamic" ErrorMessage="Empty"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="Label1" runat="server" EnableViewState="False"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
Code behind:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedItem.Text;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Add(new ListItem("Select...", string.Empty));
DropDownList1.Items.Add(new ListItem("mehdi", "1"));
DropDownList1.Items.Add(new ListItem("ali", "2"));
}
}

popupextendercontrol causes postback in asp.net 3.5

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...

ModalPopupExtender inside a GridView ItemTemplate

How do I use a GridView TemplateField containing a LinkButton that is to display the modal on click? I have rows of data that I want to update the details of when clicking the 'Edit' LinkButton in that row. There is a bunch of data I need to load via codebehind just before the Modal displays.
I was trying the following, but I can't do Modal1.Show() in the event handler because it's in a TemplateField:
<ItemTemplate>
<asp:Button runat="server" ID="HiddenForModal" style="display: none" />
<ajaxToolkit:ModalPopupExtender ID="Modal1" runat="server" TargetControlID="HiddenForModal" PopupControlID="pnlModal" />
<asp:LinkButton ID="btnEdit" runat="server" Text="Edit" onclick="btnEdit_Click" />
<asp:LinkButton ID="btnDelete" runat="server" Text="Delete"></asp:LinkButton>
</ItemTemplate>
Thanks,
Mark
The key is knowing which row in the GridView was the LinkButton that was clicked. You can do this several ways but the way I implemented it is to capture it in the RowCommand event. Then you can access the ModalPopupExtender in the clicked row via FindControl(..).
Page:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" style="Display:none;" Text="Button" />
<cc1:ModalPopupExtender ID="ModalPopupExtender1" PopupControlID="Popup1" TargetControlID="Button1" BackgroundCssClass="modalBackground" runat="server" />
<asp:LinkButton ID="LinkButton1" CommandName="Popup" runat="server">Popup</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Codebehind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
LinkButton1.CommandArgument = e.Row.RowIndex.ToString();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Popup" && e.CommandArgument != null)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
ModalPopupExtender modalPopupExtender1 = (ModalPopupExtender)GridView1.Rows[rowIndex].FindControl("ModalPopupExtender1");
modalPopupExtender1.Show();
//Perform any specific processing.
Label1.Text = string.Format("Row # {0}", rowIndex);
}
}
Additionally, because you are opening the modal on a postback anyways you don't actually need the ModalPopupExtender (or the hidden button) in the ItemTemplate. You can move that out and put it on the page (by your popup div) and can simply call the Show() method.
Page:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandName="Popup" runat="server">Popup</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Codebehind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Popup" && e.CommandArgument != null)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
ModalPopupExtender1.Show();
//Perform any specific processing
Label1.Text = string.Format("<Br>Row # {0}", rowIndex);
}
}
Thanks, good luck!

Resources