Update panel not working inside Gridview - Asp.Net/VB.Net - asp.net

When I click the image button 'imgPaymentMethod' I require the code to change the ImageUrl of the Image Button. However, nothing happens when clicked. I have also tried putting the entire gridview inside the update panel but then I get this error:
A control with ID 'imgPaymentMethod' could not be found for the
trigger in UpdatePanel 'upPnlControls'
Front End..
<asp:GridView ID="gdvItems" runat="server" AutoGenerateColumns="False"
DataKeyNames="fileID" DataSourceID="DSUploadedItems" CssClass="mGrid"
AlternatingRowStyle-CssClass="alt">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:TemplateField visible="true" HeaderText="Price">
<ItemTemplate>
<asp:DropDownList ID="ddlBuyPrice" runat="server" AutoPostBack="True" Font-Size="11px" Height="22px" OnSelectedIndexChanged="ddlBuyPrice_SelectedIndexChanged" SelectedValue='<%# Bind("buyPrice")%>'>
<asp:ListItem Value="0.00">Select:</asp:ListItem>
</asp:DropDownList>
<asp:LinkButton visible="false" ID="lnkPaymentMethod" runat="server" CausesValidation="False"
Text='<%# Bind("acceptPaypal")%>' > /></asp:LinkButton>
<br /><br />
<asp:Label ID="lblSoldStatus" visible="false" runat="server" CausesValidation="False" Text='<%# Bind("sold") %>' />
<asp:Image ID="imgSoldStatus" alt="Sold Status" runat="server" width="100px" ImageUrl="~/files/images/icons/iconSold.png" />
<br />
<asp:UpdatePanel ID="upPnlControls" style="margin-top:0px;" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<Triggers>
<asp:AsyncPostBackTrigger controlid="imgPaymentMethod" eventname="Click" />
</Triggers>
<ContentTemplate>
<asp:ImageButton visible="true" ID="imgPaymentMethod" runat="server" CausesValidation="False" width="50px" onclick="lnkPaymentMethod_Click"></asp:ImageButton>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:Label ID="lblDateBought" runat="server" CausesValidation="False" Text='<%# Bind("dateBought") %>' />
<asp:LinkButton ID="lnkMarkAsSold" runat="server" OnClick="markAsSold_Click" Text="Mark As Sold" Visible="true" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
VB Code..
Protected Sub lnkpaymentMethod_Click(sender As Object, e As System.EventArgs)
Dim imgPaymentMethod As ImageButton = CType(sender, ImageButton)
Dim currentRow As GridViewRow = DirectCast(imgPaymentMethod.Parent.Parent, GridViewRow)
If imgPaymentMethod.ImageUrl="~/files/images/icons/paypalIcon.gif" Then
imgPaymentMethod.ImageUrl="~/files/images/icons/cashIcon.gif"
Else
imgPaymentMethod.ImageUrl="~/files/images/icons/paypalIcon.gif"
End If
End Sub

After discussion we had in comments, if I understood you correctly the problem here is this line:
Dim currentRow As GridViewRow = DirectCast(imgPaymentMethod.Parent.Parent, GridViewRow)
If you want to get grid view row that you click via imgPaymentMethod button, you need to use NamingContainer instead of Parent as follow:
Dim currentRow As GridViewRow = DirectCast(imgPaymentMethod.NamingContainer, GridViewRow)

Related

DropDown binding in gridview

I have this code:
<asp:TemplateField HeaderText="Gestisci IP" SortExpression="GestisciIp">
<EditItemTemplate>
<asp:DropDownList ID="ddlGestisciIP" runat="server" AppendDataBoundItems="true"
DataSourceID="SqlDataSource3" DataTextField="GestisciIP"
DataValueField="GestisciIP" SelectedValue='<%# Bind("GestisciIP") %>'>
<asp:ListItem Text="True" Value="True" />
<asp:ListItem Text="False" Value="False" />
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("GestisciIp") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The datasource contain the value of a field selected on the current user (user id) and it can be True or False.
My problem is that I want the dropdown to always shows the 2 options True and False, but it should also have the selected value got from the datasource.
If I remove the 2 lines
<asp:ListItem Text="True" Value="True" />
<asp:ListItem Text="False" Value="False" />
it only shows the value got from the db, if I leave these lines it shows 3 values: the one got from the db and True, False added by this code.
So, how can I do this? I hope I was clear enough, thanks
IT'S DONE!
Thank you again
ASPX
<asp:TemplateField HeaderText="Gestisci IP" SortExpression="GestisciIp">
<EditItemTemplate>
<asp:DropDownList ID="ddlGestisciIP" runat="server">
</asp:DropDownList>
<asp:ListItem Text="True" Value="True" />
<asp:ListItem Text="False" Value="False" />
</asp:DropDownList>--%>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("GestisciIp") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
CODE BEHIND
Private Sub GridView2_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView2.RowCommand
If e.CommandName = "Edit" Then
Dim gvRow As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim lbl As Label = DirectCast(gvRow.FindControl("Label2"), Label)
ViewState("LabelValue") = lbl.Text
End If
End Sub
Private Sub GridView2_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView2.EditIndex = e.Row.RowIndex Then
Dim ddlGestisciIP As DropDownList = DirectCast(e.Row.FindControl("ddlGestisciIP"), DropDownList)
Dim LblddlGestisciIP As Label = DirectCast(e.Row.FindControl("LblddlGestisciIP"), Label)
ddlGestisciIP.Items.Add(New ListItem("True", "1"))
ddlGestisciIP.Items.Add(New ListItem("False", "0"))
If Convert.ToString(ViewState("LabelValue")) <> "" Then
ddlGestisciIP.SelectedValue = ddlGestisciIP.Items.FindByText(Convert.ToString(ViewState("LabelValue"))).Value
End If
End If
End Sub
Private Sub GridView2_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView2.RowUpdating
Dim ddlGestisciIP As DropDownList = DirectCast(GridView2.Rows(GridView2.EditIndex).FindControl("ddlGestisciIP"), DropDownList)
SqlDataSourceUtenti.UpdateCommand = "UPDATE [Utenti] SET [RfProfilo] = #RfProfilo, [Nome] = #Nome, [Cognome] = #Cognome, [Username] = #Username, [Password] = #Password, [Badge] = #Badge, [IpCorretto] = #IpCorretto, [GestisciIp] = " & ddlGestisciIP.SelectedValue & " WHERE [IdUtente] = #Original_IdUtente"
End Sub
try this,add Onrowdatabound event to your gridview and bind your dropdownlist in that event.
<asp:GridView ID="GridView2" runat="server" AllowPaging="True" CellPadding="4" DataSourceID="SqlDataSourceUtenti"
ForeColor="#333333" GridLines="None" AutoGenerateColumns="False"
DataKeyNames="IdUtente" BorderColor="#E1E1E1" BorderStyle="Solid"
BorderWidth="3px" OnRowDataBound="GridView2_RowDataBound" OnRowCommand="GridView2_RowCommand" OnRowUpdating="GridView2_RowUpdating" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="RfProfilo" HeaderText="RfProfilo"
SortExpression="RfProfilo" Visible="False" />
<asp:BoundField DataField="Nome" HeaderText="Nome" SortExpression="Nome" />
<asp:BoundField DataField="Cognome" HeaderText="Cognome"
SortExpression="Cognome" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:TemplateField HeaderText="Profilo" SortExpression="Profilo">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Nome" DataValueField="idProfilo"
SelectedValue='<%# Bind("RfProfilo") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("NomeProfilo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password" SortExpression="Password">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Password") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# mascheraPassword(Eval("Password").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Badge" HeaderText="Badge" SortExpression="Badge" />
<asp:TemplateField HeaderText="IP di origine" SortExpression="IpCorretto">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("IpCorretto") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblIpCorretto" maxlength="16" runat="server" Text='<%# limitaCaratteri(Eval("IpCorretto").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gestisci IP" SortExpression="GestisciIp">
<EditItemTemplate>
<asp:DropDownList ID="ddlGestisciIP" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("GestisciIp") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Aggiorna" ForeColor="White"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Annulla" ForeColor="White"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Modifica"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Elimina"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
in code behind
Protected Sub GridView2_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Edit" Then
Dim gvRow As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim lbl As Label = DirectCast(gvRow.FindControl("Label2"), Label)
ViewState("LabelValue") = lbl.Text
End If
End Sub
Private Sub GridView2_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView2.EditIndex = e.Row.RowIndex Then
Dim ddlGestisciIP As DropDownList = DirectCast(e.Row.FindControl("ddlGestisciIP"), DropDownList)
ddlGestisciIP.Items.Add(New ListItem("True", "1"))
ddlGestisciIP.Items.Add(New ListItem("False", "0"))
If Convert.ToString(ViewState("LabelValue")) <> "" Then
ddlGestisciIP.SelectedValue = ddlGestisciIP.Items.FindByText(Convert.ToString(ViewState("LabelValue"))).Value
End If
End If
End Sub
First ,With the use of RowCommand Event of the Grid View , Get the value of the Edited Label into the ViewState,then Use viewstate and Handle your RowDataBound
and to upadate your data you can use roeupdate event,like follows
protected void Gridview2_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
//add code for updating values in database
}
If i understood correct, you only want the values TRUE and FALSE, so, you don't need a datasource to do this...
Remove this properties DataSourceID, DataTextField, DataValueField and SelectedValue...
Try it please.

FindControl throwing NullReferenceException

Within my ASP gridview, I have the following (updated to show full gridview):
<asp:GridView ID="TPAnnuity_GridView" AllowSorting="true" AllowPaging="true" Runat="server"
DataSourceID="TPAnnuity_SqlDataSource" DataKeyNames="AnnuityTotalPointsID"
AutoGenerateColumns="False" ShowFooter="true" PageSize="20">
<Columns>
<asp:TemplateField HeaderText="Company" SortExpression="CompanyName" HeaderStyle-VerticalAlign="Bottom">
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("CompanyName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="EditACompanyID" runat="server" DataSource="<%# ddlCompanyDS %>" DataValueField="CompanyID" DataTextField="CompanyName" selectedValue='<%# Bind("CompanyID") %>'></asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="NewCompanyID" runat="server" DataSource="<%# ddlCompanyDS %>" DataValueField="CompanyID" DataTextField="CompanyName"></asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="NewCompanyID" Display="Dynamic" ForeColor="" ErrorMessage="You must enter a value. *" Enabled="false"></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle Wrap="False" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<br />
<i>No Commission Data to display.</i>
<br />
<br />
</EmptyDataTemplate>
</asp:GridView>
And within my back end, I have the following:
Sub TPAnnuity_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles TPAnnuity_GridView.RowCommand
If e.CommandName = "Cancel" Then
'Reset Footer Row input fields
CType(TPAnnuity_GridView.FooterRow.FindControl("NewCompanyID"), DropDownList).SelectedIndex = 0
ElseIf e.CommandName = "Insert" Then
TPAnnuity_SqlDataSource.InsertParameters.Clear()
Dim test1 As New Parameter("CompanyIDInt", TypeCode.Int32)
test1.DefaultValue = CType(TPAnnuity_GridView.FooterRow.FindControl("NewCompanyID"), DropDownList).SelectedValue
TPAnnuity_SqlDataSource.InsertParameters.Add(test1)
TPAnnuity_SqlDataSource.Insert()
ElseIf e.CommandName = "Update" Then
TPAnnuity_SqlDataSource.UpdateParameters.Clear()
Dim param1 As New Parameter("CompanyIDInt", TypeCode.Int32)
param1.DefaultValue = CType(TPAnnuity_GridView.FooterRow.FindControl("EditACompanyID"), DropDownList).SelectedValue ****THIS IS THE PROBLEM LINE****
TPAnnuity_SqlDataSource.UpdateParameters.Add(param1)
TPAnnuity_SqlDataSource.Update()
End If
End Sub
The Cancel and Insert functions work just fine, operating off of the footer. Every time hit the "Update" button, I get a NullReferenceException on the param1.Default Value = line.
Can anyone help me figure out what's going on here?
Your row should look like this:
param1.DefaultValue = CType(e.CommandSource.FindControl("EditACompanyID"), DropDownList).SelectedValue
Instead of utilizing the FooterRow, you have to use the source row. Since you passed that in with e, you can use this.

Without using SelectedIndexChanged, how to retrieve value of each row from gridview?

I have a GridView,
without using SelectedIndexChanged, how can I retrieve the value of each row from GridView when click on each button in each row?
this is my aspx code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="SqlDataSource1" ShowHeader="False" AllowPaging="True" BorderColor="White"
CellPadding="6" GridLines="None" Height="100px" Width="800px">
<Columns>
<asp:TemplateField>
<ItemTemplate>
Card Name:
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
<br />
Cost :
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
<br />
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ProductImgID") %>'></asp:Label>
<asp:Image ID="Image3" runat="server" ImageUrl='<%# Eval("ProductImgUrl", "images/{0}") %>' />
<br />
<asp:Button ID="btnAddProduct" runat="server" Text="Add" />
<br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
One option can be to Bind the CommandArgument of the button to the ProductID
<asp:Button ID="btnAddProduct" runat="server" Text="Add" CommandArgument='<%#Bind("ProductID")%>' />
and then in the RowCommand event retrieve the ProductID and extract the row from database
string prodID=(string)e.CommandArgument()
then using the ID retrieve the row from database.
To get a row value, you have to get the row Reference, After getting the row you can easily get to the specified column and can get the value
Lets Consider you have a "link button control" in a template field column. For the gridview you have to set Row Command Event, and also in the edit template of the column, set a command name for the link button also say "lnkTest"
In RowCommand Event you have to include the following section of code
if(e.CommandName.Equals("lnkTest")) // Checks that link button was clicked
{
GridViewRow grdRow = (((LinkButton)e.CommandSource).Container)
// This Will give you the reference of the Row in which the link button was clicked
int grdRowIndex = grdRow.RowIndex;
//This will give you the index of the row
var uniqueDataKeyValue = GridView1.DataKeys[grdRowIndex].Value;
//This will give you the DataKey Value for the Row in which the link Control was click
Hope the above code will help
Add CommandArgument='<%# Container.DataItemIndex %>' to your Add button
<asp:Button ID="btnAddProduct" runat="server" Text="Add" CommandArgument='<%# Container.DataItemIndex %>'/>
To retrive Name, in gridview row command use this code
Dim gvr As GridViewRow = grvGRNCONs.Rows(e.CommandArgument)
Dim name As String = DirectCast(gvr.FindControl("Label1"), Label).Text
and so on..
<asp:GridView ID="grdResults" CssClass="CommonTable dataTable" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField HeaderText="Sl#">
<ItemTemplate>
<asp:Label ID="lblSlno" Text='<%# Container.DataItemIndex+1 %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ControlStyle-Height="15px" ControlStyle-Width="15px">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" CssClass="PInfoTd" />
<ItemTemplate>
<asp:ImageButton ID="lknassesno" ToolTip="Edit Assessment" Width="50" CssClass="NewButton" ***CommandName="LINK"***
runat="server" ImageUrl="~/img/Edit.png" />
<asp:HiddenField ID="hidassesmentno" Value='<%# EVAL("PAN_CODE")%>' runat="server" />
<asp:HiddenField ID="hidPendStatus" Value='<%# EVAL("Pstatus")%>' runat="server" />
<asp:HiddenField ID="hidIPNO"Value='<%#EVAL("IP_NO")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
**code behind**
Protected Sub grdResults_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdResults.RowCommand
If **e.CommandName = "LINK"** Then
Dim ctrl As Control = e.CommandSource
Dim grrow As GridViewRow = ctrl.Parent.NamingContainer
'Dim i As Integer = Convert.ToInt16(e.CommandArgument)
'Dim lknassesno As HiddenField = DirectCast(e.CommandSource, ImageButton)
Dim hidAssesmentNo As HiddenField = DirectCast(grdResults.Rows(grrow.RowIndex).FindControl("hidassesmentno"), HiddenField)
Dim lblstatus As HiddenField = DirectCast(grdResults.Rows(grrow.RowIndex).FindControl("hidPendStatus"), HiddenField)
Dim hidIpNo As HiddenField = DirectCast(grdResults.Rows(grrow.RowIndex).FindControl("hidIPNO"), HiddenField)
Dim Assno As String = hidAssesmentNo.Value
Dim Ipno As String = hidIpNo.Value
Dim st As String = ""
If lblstatus.Value = "Pending" Then
st = "E"`enter code here`
ElseIf lblstatus.Value = "Completed" Then
st = "V"
End If
Response.Redirect("Assessment.aspx?PAssNo=" & Assno & "&Mode=" & st & "&IPNO=" & Ipno & "")
End If
End Sub

ASP.NET - Adding an UpdatePanel trigger to a LinkButton inside a gridview

I was trying to update the content of a modal dialog, and this code works for me:
<asp:LinkButton ID="updateSomething" runat="server" Text="Update" CausesValidation="false" OnClientClick="openDialog();" onclick="UpdateButton_Click" />
<asp:UpdatePanel ID="upNewUpdatePanel" runat="server">
<ContentTemplate>
<asp:Label ID="updateLabel" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="updateSomething" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
However, when I try to place the LinkButton inside a gridview, like so:
<asp:GridView ID="grdListUsers" runat="server" AutoGenerateColumns="false" AllowPaging="false" OnRowDataBound="grdRowDefListUsers" CssClass="mGrid" EmptyDataText="No users.">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Nome" HeaderStyle-Width="300" />
<asp:BoundField DataField="Login" HeaderText="Login" HeaderStyle-Width="300" />
<asp:TemplateField HeaderText="Options" HeaderStyle-Width="75" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:LinkButton ID="updateSomething" runat="server" Text="Update" CausesValidation="false" OnClientClick="openDialog();" onclick="UpdateButton_Click" />
</asp:TemplateField>
</Columns>
</asp:GridView>
This does not work, I get an error saying: A control with ID 'updateSomething' could not be found for the trigger in UpdatePanel 'upNewUpdatePanel'.
How can I use the ImageButton inside the gridview?
Try and add the asp:AsyncPostBackTrigger to the asp:GridView's OnRowCommand event and handle the link button click in that event
<asp:GridView ID="grdListUsers" runat="server" onRowCommand="grdListUsers_RowCommand">
<asp:TemplateField>
<asp:LinkButton ID="updateSomething" CommandName="update-something" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'/>
</asp:TemplateField>
</asp:GridView>
and in the cs create the event like this
protected void grdListUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "update-something")
{
grdListUsers.SelectedIndex = Convert.ToInt32(e.CommandArgument);
}
}
You could add a trigger of the gridview
<Triggers>
<asp:PostBackTrigger ControlID="gridview1" />
</Triggers>
Add another Update Panel surrounding your link button just like below.
<asp:GridView ID="grdListUsers" runat="server" AutoGenerateColumns="false" AllowPaging="false" OnRowDataBound="grdRowDefListUsers" CssClass="mGrid" EmptyDataText="No users.">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Nome" HeaderStyle-Width="300" />
<asp:BoundField DataField="Login" HeaderText="Login" HeaderStyle-Width="300" />
<asp:TemplateField HeaderText="Options" HeaderStyle-Width="75" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:UpdatePanel ID="aa" runat="server">
<ContentTemplate>
<asp:LinkButton ID="updateSomething" runat="server" Text="Update" CausesValidation="false" OnClientClick="openDialog();" onclick="UpdateButton_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="updateSomething"/>
</Triggers>
</asp:UpdatePanel>
</asp:TemplateField>
</Columns>
</asp:GridView>
You could set the UpdatePanel's UpdateMode to Conditional and update it manually from the UpdateButton_Click-Handler:
<asp:UpdatePanel ID="UdpFormPanel" runat="server" UpdateMode="conditional" ChildrenAsTriggers="false" >
LinkButton's Click-Event handler:
Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As EventArgs)
'blah....
upNewUpdatePanel.Update()
End Sub

ASP.NET (VB): GridView with ImageButton > Populate text box with comments

It has been so long since I have coded a page in VB. For the life of me I just cannot remember how to do this.
I have a GridView on an ASP page. Each row has a comment ImageButton. I already have the gridview and text box wrapped in a UpdatePanel. The gridview shows all of the correct information. I just need to add some code to populate the text box with the comment when a user clicks on that row's ImageButton.
The comments are stored in SQL 2005 DB if that makes a difference. Should I shove the comments inside a hidden field of the gridview or is there a function that will allow me to query the db for that specific comment.
End goal would be to not refresh the page if possible.
Your help is greatly appreciated to help me get over this writer's block!
OK, you could do this either way, with a HiddenField or by looking up in the DB.
HiddenField
In the ItemTemplate that contains the ImageButton, add CommandName and CommandArgument attributes to the ImageButton, and a HiddenField that is bound to the comment field from the database:
<asp:TemplateField HeaderText="Comment">
<ItemTemplate>
<asp:ImageButton ID="btnComment" runat="server" ImageUrl="images/comment.gif" CommandName="SelectComment" CommandArgument='<%# Container.DataItemIndex %>' />
<asp:HiddenField runat="server" id="CommentHiddenField" Value='<%# Eval("Comment") %>' />
</ItemTemplate>
</asp:TemplateField>
In the code-behind, add a method for handling the RowCommand event for the GridView:
Private Sub Gridview2_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles Gridview2.RowCommand
Dim rowIndex As Integer
Dim commentHiddenField As HiddenField
If e.CommandName = "SelectComment" Then
rowIndex = Integer.Parse(e.CommandArgument.ToString)
commentHiddenField = DirectCast(Gridview1.Rows(rowIndex).Cells(5).FindControl("CommentHiddenField"), HiddenField)
txtComments.Text = commentHiddenField.Value
End If
End Sub
DB Lookup
Add the attributes to the ImageButton:
<asp:TemplateField HeaderText="Comment">
<ItemTemplate>
<asp:ImageButton ID="btnComment" runat="server" ImageUrl="images/comment.gif" CommandName="SelectComment" CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
In the code-behind, add a method for handling the RowCommand event for the GridView:
Private Sub Gridview2_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles Gridview2.RowCommand
Dim rowIndex As Integer
Dim key As String
rowIndex = Integer.Parse(e.CommandArgument.ToString)
key = Gridview1.DataKeys(rowIndex).Value.ToString
txtComments.Text = GetCommentFromDB(key)
End Sub
Here is the markup...
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<tr class="dvrow" align="center">
<td style="text-align:left;" colspan="2">History<br />
<div id="div1" style="width:600px;">
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
GridLines="None" DataKeyNames="RepIssueHistoryID"
DataSourceID="sqlIssueHistory" Width="600px" AllowSorting="True"
AllowPaging="True" CssClass="grid" RowStyle-Height="15px">
<PagerStyle CssClass="footer" />
<Columns>
<asp:TemplateField HeaderText="Mail">
<ItemTemplate>
<asp:CheckBox ID="chkMailComment" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RepIssueStatus" SortExpression="RepIssueStatus"
HeaderText="Status" ItemStyle-Width="120px" >
<ItemStyle Width="120px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="RepIssuePriority" SortExpression="RepIssuePriority"
HeaderText="Priority" ItemStyle-Width="100px" >
<ItemStyle Width="100px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="User" SortExpression="User" HeaderText="Rep"
ItemStyle-Width="180px" >
<ItemStyle Width="180px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="DateUpdate" SortExpression="DateUpdate"
HeaderText="Date" ItemStyle-Width="200px" >
<ItemStyle Width="200px"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Comment">
<ItemTemplate>
<asp:ImageButton ID="btnComment" runat="server" ImageUrl="images/comment.gif" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Attachment">
<ItemTemplate>
<asp:ImageButton ID="btnAttachment" runat="server" ImageUrl="images/folder.gif" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No history currently exists.<br />
</EmptyDataTemplate>
<RowStyle Height="15px"></RowStyle>
<EmptyDataRowStyle CssClass="empty" />
</asp:GridView>
</div>
</td>
</tr>
<tr class="dvrow" align="center">
<td style="text-align:left;" colspan="2">Rep Comment<br />
<asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine"
Width="600px" Height="180px" ReadOnly="true" />
</td>
</tr>
</ContentTemplate>
</asp:UpdatePanel>
So the basic idea would be for someone to click on btnComment on whichever row and that comment would then show up in txtComment. Hope that explains it better.

Resources