4

I have a GridView that has 3 columns: FirstName, LastName and a TemplateField FullName where I string together FirstName and LastName.

Assuming calling DisplayFullName is the function I want to use to concatenate FirstName and LastName, how do I pass the row argument to the function and how to declare the parameter of the function? Thanks.

Here's my code for the FullName column:

        <asp:TemplateField HeaderText="FullName">
            <ItemTemplate>
                <%# DisplayFullName(???) %>
            </ItemTemplate>
        </asp:TemplateField>

Here's my declaration for the function:

protected string DisplayFullName(???)
  { ... }

The ??? are where I need help. OR do I need to pass the row at all? If each time DisplayFullName is called, the 'current' row is known. If so, how do I access the current row in DisplayFullName?

I simplified the operation for the sake of clarity of the question. In reality, there can be up to 20 values in the row I need and I want to do some calculations in the function called.

RJIGO
  • 1,903
  • 7
  • 22
  • 31

3 Answers3

3

Had to post since nobody seemed to give you an answer that does exactly what you ask for - passing the row to a backend function.

Simply pass "Container.DataItem" to your function - this is the DataRow

<asp:TemplateField>
    <ItemTemplate> 
        <%# DisplayFullName(Container.DataItem) %>
    </ItemTemplate> 
</asp:TemplateField>

then in the CodeBehind you can manipulate it as a DataRowView:

public string DisplayFullName(object containerDataItem)
{
    string firstName = ((DataRowView)containerDataItem)["FirstName"].ToString();
    string lastName = ((DataRowView)containerDataItem)["LastName"].ToString();
    ...

Hope this helps!

Qjimbo
  • 377
  • 2
  • 11
1

I don't know why you are calling a function here to display full name. You can use like this in your code to achieve full name:

    <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <%#Eval("FirstName") %>&nbsp;<%#Eval("LastName") %>   
                </ItemTemplate>
            </asp:TemplateField>
</asp:TemplateField>
Neha
  • 2,933
  • 3
  • 19
  • 23
  • I added a clarification in my question. The scenario is more complicated. In reality, I need to do some calculations for the TemplateField. – RJIGO Apr 04 '12 at 05:19
  • <%# FunctionName(parameter1, parameter2, ..., parameterN) %> call your function like this. the parameter may be your rowid or firstname, last name anything. – Neha Apr 04 '12 at 05:21
  • This is not what I want to do. Because all the arguments are contained in the row. There can be up to 20 arguments. So all I should pass is the row then extract all the data from the row in the called function. – RJIGO Apr 04 '12 at 05:27
  • Then simply pass your data key in function & then get your items. Refer this.. might be help you http://forums.asp.net/t/1645113.aspx/1 – Neha Apr 04 '12 at 05:31
1

@RJIGO: You can use function like this:

<asp:TemplateField HeaderText="Name">
     <ItemTemplate>
        <%# DisplayFullName(Eval("FirstName"), Eval("LastName"))%>         
        </ItemTemplate>
     </asp:TemplateField>

and your code behind method will like this

  protected string DisplayFullName(object FirstName, object LastName)
  {
      return Convert.ToString(FirstName)+Convert.ToString(LastName);
  }
R.D.
  • 7,153
  • 7
  • 22
  • 26