Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to click twice(!) for LinkButton

Status
Not open for further replies.

RebLazer

Programmer
Jun 7, 2002
438
US
I have a table in which the first column is that of LinkButtons. The problem is that when the user clicks on one of the LinkButtons, it makes that little "postback click", but that's it. It's only on the *second* click it actually executes the appropriate procedure.

Here's the code for the redirect sub (the LinkButton of interest is "select" with method "BindDetailGrid":
[tt]

Sub DataGrid_Select(Sender As Object, E As datagridcommandEventArgs)
trace.warn("enter DataGrid_Select - command name =")
trace.warn(e.commandname)
select case e.commandname
case "select"
BindDetailGrid(e)
case "select_pi"
bindPIgrid(e)
case "grants_in_01"
grants_in(e,"I",2001)
case "grants_all_01"
grants_in(e,"%",2001)
case "grants_in_02"
grants_in(e,"I",2002)
case "grants_all_02"
grants_in(e,"%",2002)
end select
End Sub
[/tt]


and here is the sub "BindDetailGrid":[tt]
Sub BindDetailGrid(e)
trace.warn("enter BindDetailGrid")
'trace.warn("DataGrid1.SelectedIndex=" & cstr(e.item.itemIndex))
if LastOrg = String.Empty then
lastOrg=CStr(DataGrid1.DataKeys(e.item.itemIndex)).Replace("'", "''")
DetailsGrid.CurrentPageIndex = 0
else
if LastOrg <> CStr(DataGrid1.DataKeys(e.item.itemIndex)).Replace(&quot;'&quot;, &quot;''&quot;) then
lastOrg=CStr(DataGrid1.DataKeys(e.item.itemIndex)).Replace(&quot;'&quot;, &quot;''&quot;)
DetailsGrid.CurrentPageIndex = 0
end if
end if
trace.warn(&quot;LastOrg=&quot;+lastorg)
trace.warn(&quot;selected index = &quot;+cstr(DataGrid1.SelectedIndex))

' get the filter value from the master Grid's DataKeys collection
If DataGrid1.SelectedIndex <> -1 Then
org_name.text=LastOrg
org_name.visible=true
DetailsGrid.visible=true
ShowOrg.visible=true
DataGrid1.visible=false
in_state.visible = false
in_org.visible=false
org_lbl.visible=false
st_lbl.visible=false
go_button.visible=false
Dim ConnectionString As String = ConfigurationSettings.AppSettings(&quot;ConnectstrW&quot;)
Dim filterValue As String = LastOrg
Dim CommandText As String
If SortField2 = String.Empty Then
CommandText = &quot;select grant_yr,grant_title,Grant_#,grant_amt,decision,pi_first_nm,pi_last_nm from grant_info where org_id = '&quot; & filterValue & &quot;' order by decision, grant_amt desc&quot;
else
CommandText = &quot;select grant_yr,grant_title,Grant_#,grant_amt,decision,pi_first_nm,pi_last_nm from grant_info where org_id = '&quot; & filterValue & &quot;' order by &quot; & SortField2
if sortasc=&quot;NO&quot; then
CommandText += &quot; DESC&quot;
end if
end if
trace.warn(&quot; BindDetailGrid CommandText=&quot; & cstr(CommandText))

Dim myConnection As New SqlConnection(ConnectionString)
Dim myCommand As New SqlDataAdapter(CommandText, myConnection)

dim ds2 as New DataSet()
myCommand.Fill(ds2)


DetailsGrid.DataSource = ds2
DetailsGrid.DataBind()
End If
End Sub
[/tt]


Thanks very much!
RebLazer
 
What's the buttonColumn declaration look like in your interface file?
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
<asp:ButtonColumn Text=&quot;Grants&quot; HeaderText=&quot;Select&quot; CommandName=&quot;select&quot;>
<HeaderStyle width=&quot;40px&quot;></HeaderStyle>
<ItemStyle font-size=&quot;Smaller&quot; font-bold=&quot;True&quot;></ItemStyle>
</asp:ButtonColumn>

<asp:ButtonColumn Text=&quot;PI&quot; HeaderText=&quot;Select&quot; CommandName=&quot;select_pi&quot;></asp:ButtonColumn>




Please notice both of these button columns. The first button column is problematic - the second one works just fine.

I've spent a bunch of hours debugging this and I have found the following: When &quot;Grants&quot; is clicked, it is supposed to set a variable &quot;selectedIndex&quot; to the row number of the selection based on the datagridcommandEventArgs. The first time it is clicked, the value of &quot;selectedIndex&quot; doesn't change at all. The second time it is clicked, it comes into that exact same sub (&quot;BindDetailGrid&quot;) with the proper value (i.e. the row that was clicked). All the routines and processes it goes through the first and second time are identical - just that it works the second time, but not the first.

Is this a bug in ASP.NET, or something? I am using WebMatrix as my IDE - don't have VS...

Thanks so much!
RebLazer
 
P.S. ...I already tried removing the formatting from the first LinkButton column

namely -
<HeaderStyle width=&quot;40px&quot;></HeaderStyle>
<ItemStyle font-size=&quot;Smaller&quot; font-bold=&quot;True&quot;></ItemStyle>

which the second LinkButton column doesn't have - but that didn't help...
 
So I don't see where you're wiring that event to the button... where is your onCommand declaration for the button?

< ...onCommand=&quot;myEventHandler&quot; />

or

protected sub myEventHandler(...) handles myButton.click

??
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
All that was wrong was a &quot;-1&quot; needed to be changed to a &quot;0&quot;:

Sub Page_Load(Sender As Object, E As EventArgs)
trace.warn(&quot;enter Page_Load&quot;)

If Not Page.IsPostBack Then
trace.warn(&quot;Page_Load inside if&quot;)

' Databind the data grid on the first request only
' (on postback, rebind only in paging and sorting commands)
DataGrid1.SelectedIndex = -1 'this must be &quot;=0&quot; instead!!! I don't know why - but it works...
DataGrid1.CurrentPageIndex = 0
DetailsGrid.CurrentPageIndex = 0

End If

End Sub
 
So your problem is solved?

If not, then I do see your point on my last post, and I was a bit lacking on the specifics, but if you want an event wired to the &quot;Edit&quot; command, for instance, then you do have to specifically wire an event handler to that event...

You can read about this in various datagrid tutorials, one of the best of which being from 4guysfromrolla, that you can find linked in the following thread:

thread855-447401

penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top