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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a variable through OnClick 2

Status
Not open for further replies.

skispray

Programmer
Feb 28, 2003
16
0
0
US
I have a datagrid with a button in a column. Looks like this:
ID | text | bit value | button

When the button is clicked, I'd like to change the bit value for that record. I'm having a lot of trouble passing the number in the ID column to the event the button calls in the OnClick. I tried calling the UpdateDB(byval ID) function using onclick="UpdateDB(ID)" but it returns an error.

Any ideas on how I can do this?
 
I think I can help you If you post the code
of your datagrid definition and the function you are calling on button-click.
 
Thanks, here's the code:

UpdateDB(byval ID)
Dim ConnectionString As String = Configur...
Dim CommandText As String = "update tbl1 set bit_col='1' where id_col='" & ID & "'"

Dim myConnection As New SqlConnection(ConnectionString)
Dim myCommand As New SqlCommand(CommandText,myConnection)
myConnection.Open()
myCommand.ExecuteReader
myConnection.Close()

The trick is getting the ID from a column in the datagrid over to this function so I can update the DB. I tried using onclick=&quot;UpdateDB(<% varID %>)&quot; but it doesn't work.
 
For that Button You can keep one Button column in your DataGrid.

The click of button in that column will raise Datagrids ItemCommand event.In that event you can capture the particular row containing the button clicked, so that you can get all other column data for that row.

I think this sui for your requirement

Example code:

<asp:datagrid id=&quot;DataGrid1&quot; style=&quot;Z-INDEX: 101; LEFT: 186px; POSITION: absolute; TOP: 136px&quot; runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot; Width=&quot;190px&quot; Height=&quot;220px&quot; Runat=&quot;server&quot; OnItemCommand=&quot;DataGrid1_ItemCommand&quot; AllowPaging=&quot;True&quot; DataKeyField=&quot;EmpCode&quot;>
<SelectedItemStyle BackColor=&quot;#FF8000&quot;></SelectedItemStyle>
<Columns>
<asp:BoundColumn DataField=&quot;EmpCode&quot;></asp:BoundColumn>
<asp:TemplateColumn>

</asp:TemplateColumn>
<asp:ButtonColumn Text=&quot;Update&quot; ButtonType=&quot;PushButton&quot; CommandName=&quot;Update&quot;></asp:ButtonColumn>
</Columns>
</asp:datagrid>




CodeBehind:



Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
Dim ad As New SqlDataAdapter(&quot;select * from Employee&quot;, &quot;server=microsoftserver;initial catalog=winningworld;uid= Dim ds As New DataSet()
ad.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End If
End Sub

Public Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
Response.Write(&quot;ItemCommand&quot;)

Dim id As String
id = DataGrid1.Items(e.Item.ItemIndex).Cells(0).Text
Response.Write(id)
End Sub
 
If you already have the EmpCode as DataKeyField in your bound grid, then it's not neccesary to have an additional BoundColumn, holding the same value(in your case, it will be DataKeyField=&quot;id_col&quot;). Like mbhavani said, the EmpCode can be referenced in the ItemCommand event but a little simplier:
Code:
Dim id As String
id = DataGrid1.DataKeys(e.Item.ItemIndex).ToString()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top