开发者

ASP.NET + DataBinder.Eval with Multiple Bound Fields

I have a GridView that shows a list of users. Each user has a first name, last name, and other data you would expect a user to have. Each value is exposed through BoundField. For instance, first name is available through "FirstName", last name is available by "LastName".

When a user clicks a LinkButton I have in a TemplateField, I need to execute some 开发者_运维百科JavaScript. This JavaScript function requires both the FirstName and LastName. Currently, I am trying something like this:

<asp:LinkButton ID="lbPrompt" runat="server" Text="prompt" CommandName="Prompt"  
  CommandArgument='<%# Eval("UserName") %>' 
  OnClientClick='<%# Eval("FirstName", "LastName", "return askUser(\"{0}\",\"{1}\");") %>' />

This statement does not work because the Eval method does not accept this kind of overload. How do I accomplish this task?

Thanks


<%# String.Format(
      "return askUser(\"{0}\",\"{1}\")", 
       Eval("FirstName"), 
       Eval("LastName")) %>


Try

OnClientClick='<%# "return askUser(\"" + Eval("FirstName") + "\",\"" + Eval("LastName") + "\");" %>'

(untested)


In case you are using code behind, you can have it in following event.

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        LinkButton lb = e.Row.FindControl("lbPrompt") as LinkButton;
        if (lb != null)
        {
            string firstName = Convert.ToString(DataBinder.Eval(DataBinder.GetDataItem(e.Row), "FirstName"));
            string lastName = Convert.ToString(DataBinder.Eval(DataBinder.GetDataItem(e.Row), "LastName"));

            lb.Attributes.Add("OnClick", "javascript:return askUser(" + firstName + ", " + lastName + ")");
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜