• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 870
  • Last Modified:

Link button requires two clicks to refresh page

I have an update panel.  Inside the panel there is a gridview.  The gridview has several rows and each row has a dropdown list and a link button.  

The link button hides the current panel and shows another panel based on what the user selected in the drop down.  

I am trying to trap when the user does not select anything in the drop down list and pop an alert.  

The alert message code from the code behind:
 Protected Sub gvcadence_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvcadence.RowCommand

If e.CommandName = "Change" Then
  If ddlCadence.SelectedIndex = -1 Then
    str.Append("<script type=""text/javascript"" language=""javascript"">" & vbCr)
    str.Append("" & vbCr)
    str.Append("alert(You Must Select A Cadence Name.);" & vbCr)
    str.Append("" & vbCr)
    str.Append("" & vbCr)
    str.Append("</script>" & vbCr)
    ScriptManager.RegisterClientScriptBlock(Me, Me.gvcadence.GetType(), Guid.NewGuid().ToString(), str.ToString, False)
  End if
End if         

Open in new window

This what the UI looks like
 <asp:updatepanel id="upSubCategories" runat="server"  >
 <contenttemplate>
<asp:gridview id=gvcadence runat="server" AutoGenerateColumns="false" OnRowEditing="gvcadence_RowEditing" >
// other controls have been omitted
<asp:LinkButton ID="btnChangeCadence"  Text="Change"  runat="server" CommandName="Change" onClick="btnChangeCadence_Click" >
</asp:LInkButton>
</asp:gridview>
</contenttemplate>
</asp:undatepanel>

Open in new window

This is the codebehind for the onClick Event
Protected Sub btnChangeCadence_Click(sender As Object, e As System.EventArgs) Handles btnChangeCadence.Click
            Me.upHeader.Visible = False
            Me.upSubCategories.Visible = False

            Me.lnkEdit.Visible = True
            Me.lnkClose.Visible = True
            BindCadenceGrid()
            Me.upChgPanel.Visible = True

Open in new window

When I don't select anything from the drop down then I get an alert, but when I select something in the dropdown list, then I have to click on the button twice in order for the next panel to become visible.  

I can't figure out what is making me have to click twice.  

Can someone take a look.  Much appreciated.
0
sherbug1015
Asked:
sherbug1015
1 Solution
 
Roopesh ReddyCommented:
Hi,

In the first place, it's better to validate DropDownList at client side than on server side -
http://roopeshreddy.wordpress.com/2012/06/09/asp-net-sever-controls-validation-using-javascript-jquery/

Hope it helps u...
0
 
Easwaran ParamasivamCommented:
0
 
CodeCruiserCommented:
Change your rowcommand sub to following and remove the click handler of linkbutton


Protected Sub gvcadence_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvcadence.RowCommand

If e.CommandName = "Change" Then
                      If ddlCadence.SelectedIndex = -1 Then
                          str.Append("<script type=""text/javascript"" language=""javascript"">" & vbCr)
                          str.Append("" & vbCr)
                          str.Append("alert(You Must Select A Cadence Name.);" & vbCr)
                          str.Append("" & vbCr)
                          str.Append("" & vbCr)
                          str.Append("</script>" & vbCr)
                          ScriptManager.RegisterClientScriptBlock(Me, Me.gvcadence.GetType(), Guid.NewGuid().ToString(), str.ToString, False)
            Else
                          Me.upHeader.Visible = False
                          Me.upSubCategories.Visible = False

                          Me.lnkEdit.Visible = True
                          Me.lnkClose.Visible = True
                          BindCadenceGrid()
                          Me.upChgPanel.Visible = True
            End if
End if         
End Sub

Open in new window

0
 
Michel PlungjanIT ExpertCommented:
You do need quotes in the script but why the extreme formatting of the script?

str.Append("<script type=""text/javascript"">alert('You Must Select A Cadence Name.');</script>" & vbCr)

is a lot easier to read
0
 
sherbug1015Author Commented:
I think this did the trick.  Thanks.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now