Problems with confirm on ASP.NET - asp.net

This is very weird, i have an asp:ImageButton inside a list view and i want to confirm and action before i execute the method but i can't
then i created another imagebutton outside the list view and the confirm does work can u guys tell me why?
this doesn't work
<asp:ImageButton ID="btnEnabled" OnCommand="CommandExecution" OnClientClick="return confirm('Desea eliminar el cliente ?');" ImageUrl="~/App_Themes/Default/images/Habilitar.png"
Visible='<%# Eval("IsEnabled") %>' CommandName="ChangeStatus" ToolTip="Deshabilitar distribuidor"
runat="server" CommandArgument='<%# Eval("IdClient") %>' />
this one does
<asp:ImageButton ID="btnConfirmacion3" ImageUrl="~/App_Themes/Default/images/Habilitar.png" runat="server" Text="Confirmacion 3" OnClientClick="return confirm('Desea eliminar el cliente ?');" />

Set AutoPostback to false. then create two hidden textboxes for command name and params. then place a hidden linkbutton in page. in onclientclick event of your image buttons after confirm place command name and params in hidden boxes and then call postback method of link button. then handle command in link button event handler.

Related

ASP.NET confirmation box showing twice

I've searched for a way to display a confirmation box when a certain button is pressed.
The only problem I have, is that the confirmation box shows twice.
Code:
<dx:ASPxButton ID="btnDel" runat="server" onClick="btnDel_Click" Text="Delete">
and in Page_Load:
btnDel.Attributes.Add("onclick", "return confirm('Are you sure?');");
Change your Page_Load code to:
if (! IsPostback)
btnDel.Attributes.Add("onclick", "return confirm('Are you sure?');");
You only have to add the onclick attribute once at the first load of the page.
the button control you are using is devexpress button and it implements it's own ClientSideEventCollection in which you can add any javascript. you can do it like following
<dx:ASPxButton ID="btnDel" runat="server" onClick="btnDel_Click" Text="Delete">
<ClientSideEvents Click="function(s, e) {
e.processOnServer = confirm('Are sure to save data?');
}" />
</dx:ASPxButton>
Here e.processOnServer is a boolean which decides if the server trip should be executed, so we assigned the result of confirm to this boolean.
you can find more about ClientSideEvents on a dev express control, in the devexpress docs here

Stop Postback in Image Button after validation failed

I have a aspx page containing a text box and an image button for search. I have used compare validator (to check for integer values) with the textbox. But the page reloads on the image button click even if I enter alphanumeric characters, along with showing the error message.
I tried using a regularexpressionvalidator instead but the problem persists.
But when i used a simple asp:button instead and binded it with textbox validation, its working fine (i.e. postback does not occur on incorrect value of textbox) and same is true with dropdownlist also (no postback occuring).
Please suggest.
Here's the code-
#peroija : Here's the code
<asp:ImageButton ID="btnSearch" runat="server" OnClick="btnSearch_Click"
ToolTip="Search" ValidationGroup="valControl" CausesValidation="true" />
<asp:TextBox ID="txtWidth" CssClass="TextFont" runat="server"
Width="233px" MaxLength="20"
ValidationGroup="valControl" CausesValidation="true"></asp:TextBox>
<asp:CompareValidator runat="server" ID="cmpValWidth"
ErrorMessage="Please enter integer values" ControlToValidate="txtWidth" Display="Dynamic"
Operator="DataTypeCheck" ValidationGroup="valControl"Type="Integer"/>
Sounds to me like you need to write
if(!isPostBack)
{
"your code"
}
in the code behind. To prevent the code from being run if the page is not viewed for the first time
Remove this from your textbox, you only need it on the validator and the button:
ValidationGroup="valControl" CausesValidation="true"
If javascript is disabled, then there will be no client side validation, so always check the validity on the server side also:
if(Page.IsValid)
{
"your btnSearch_Click code"
}

Field validation of a single button only

I have the following validator on a textbox inside a modal dialog box.
<asp:RequiredFieldValidator runat = "server"
ErrorMessage = "Role name can not be empty."
ControlToValidate = "tbxRoleName" />
It works like it should, except that the validation triggers on every other buttons OnClick handler on the aspx-page too. Since the dialog is invisible it looks like buttons just dont work on the page. My workaround is to add CausesValidation = "false" on all buttons on the page. But it is a very bad solution and I think there should be a smarter way.
Assign ValidationGroup to each validator and also to the button that should trigger validation (but not the the other button). Something like:
<asp:RequiredFieldValidator ValidationGroup='valGroup1' ... />
<asp:Button ValidationGroup='valGroup1' Text='I trigger validation' ... />
How about setting a ValidationGroup?
http://msdn.microsoft.com/en-us/library/ms227424.aspx
Also you can use 'causesvalidation' to the button. If it is false Button will not response to Validation in aspx page.
Example:
<asp:Button runat="server" Text="Cancel" CausesValidation="false" />
The Button has a property CausesValidation that can disable the validation for that button.
More info here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.causesvalidation.aspx

How to add a hyperlink to my gridview

I want to set hyperlink (on image) in my gridview. When user clicks on that hyperlink, a query string should be generated based on selected value of dropdown list. How to set the hyperlink in gridview and how to form query string for that hyperlink?
Thanks in advance..
You can simply Cancatinate the value of your dropdown to NavigateUrl property of hyperlink
<ItemTemplate>
<asp:HyperLink ID="hlEdit" runat="server"
NavigateUrl='<%# Eval("ID", "PageName.aspx?ID={0}" + "&TID=" + ddl.SelectedValue) %>'
ImageUrl="~/Images/edit.png"></asp:HyperLink>
</ItemTemplate>
Edit:
<ItemTemplate>
<asp:ImageButton ID="hlEdit" runat="server"
PostBackUrl='<%# Eval("ID", "PageName.aspx?ID={0}" + "&TID=" + ddl.SelectedValue) %>'
ImageUrl="~/Images/edit.png"></asp:ImageButton>
</ItemTemplate>
You will probably need javascript for this.
Add an 'onclick' attribute to your images
In the onclick handler, you retrieve the dropdownlist value and compose your query
Set the composed url to the href of your link
Some more detailed information would be useful to be able to provide you with some code..
Are you using an asp HyperLink, ImageButton, ...?
You could for example use the OnClientClick property in case you would be using an ImageButton.

FindControl("someTextBox") in GridView not sending the updated value

Im populating a GridView from List so am forced to use TemplateField controls to allow editing. This requires displaying a TextBox populated with the original value when in edit mode and using FindControl to get the new value out on update submit.
Problem is foundTextBox.Text == "OriginalTextBoxValue"
<asp:TemplateField HeaderText="A Field">
<ItemTemplate>
<asp:Label ID="_theLabel" runat="server" Text='<%# Eval("AField") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="_theTextBox" runat="server" Text='<%# Eval("AField") %>' />
</EditItemTemplate>
</asp:TemplateField>
And the code in my update event handler
TextBox newText = (TextBox)_myGridView.Rows[e.RowIndex].FindControl("_thTextBox");
//newText.Text == the old value of the text box
Is your gridview binded at every postback? This could explain why you never get the updated value, because the gridview is rebinded before reading the textbox.
Could you paste your complete update method?
You've got the code behind in the wrong event handler. Move it to the Editing event handler, so it will populate the textbox whenever the user clicks on the Edit command for a row.

Resources