user1477388
user1477388

Reputation: 21440

Accessing Row in GridView

I have the following ASPX code:

<ItemTemplate>
    <asp:HyperLink ID="BRANCH_NAME" runat="server" 
          NavigateUrl='<%# "mailto:"+Eval("OWNERS_EMAIL") %>' 
          Text='<%# Eval("BRANCH_NAME") %>' 
          ToolTip='<%# GetBranchAddress() %> '> 
    </asp:HyperLink>
</ItemTemplate>

The function in the code behind:

protected string GetBranchAddress(DataRow row)
{
    return "<span style=\"text-decoration:underline;\">"+ 
               row["BRANCH_NAME"].ToString().Trim() + "</span><br />" + 
               row["OWNERS_FIRST_NAME"].ToString().Trim() + " " +
               row["OWNERS_LAST_NAME"].ToString().Trim() + "<br />" + 
               row["OWNERS_EMAIL"].ToString().Trim() + "<br />" + 
               row["OWNERS_OFFICE_PHONE"].ToString().Trim() + "<br />" + 
               row["OWNERS_FAX_PHONE"].ToString().Trim();

}

The error I get is:

No overload for method 'GetBranchAddress' takes '0' arguments

How can I access row in my ASP GridView so that I can pass it to my function?

Thanks.

Upvotes: 0

Views: 290

Answers (2)

James Johnson
James Johnson

Reputation: 46077

Instead of trying to pass the entire DataRow into the function, utilize datakeys to specify the fields you want to display, and pass in the row index.

<asp:GridView ID="GridView1" runat="server" DataKeyNames="BRANCH_NAME, StreetAddress" ...>

The code in the ItemTemplate would look something like this:

<ItemTemplate>
    <asp:HyperLink ID="BRANCH_NAME" runat="server" 
          NavigateUrl='<%# "mailto:"+Eval("OWNERS_EMAIL") %>' 
          Text='<%# Eval("BRANCH_NAME") %>' 
          ToolTip='<%# GetBranchAddress(Container.DisplayIndex) %> '> 
    </asp:HyperLink>
</ItemTemplate>

In your function, you can access the datakey values like this:

protected string GetBranchAddress(int rowIndex)
{
    return GridView1.DataKeys[rowIndex]["BRANCH_NAME"].ToString();
}

Upvotes: 2

Mike Marks
Mike Marks

Reputation: 10139

In your markup, you're not passing any parameters in to GetBranchAddress, which is why you're getting the error. You should pass "this" into the method, where "this" is the currently operated on object (the DataRow). For example,

ToolTip='<%# GetBranchAddress(this) %> '

Upvotes: 0

Related Questions