Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
ASP.NET
<asp:GridView ID="Grd" runat="server" AutoGenerateColumns="false" CssClass="GridStyle"                                                                HeaderStyle-Font-Size="Small" Width="1001px"  Height="300px">
<Columns>
<asp:TemplateField HeaderStyle-Width="160px" Visible="false" HeaderText="Action">
<ItemTemplate>

<asp:Button ID="btnProcess" runat="server" Text="Place" OnClick="btnProcess_Click" 
CommandArgument='<%#Eval("intId") & "~" & Eval("IdECompleted") & "~" & Eval("dtECompletion") & "~" & Eval("strName") & " " & Eval("strCode") & " " & Eval("strAge") & " " & Eval("strGender") & "~" & Eval("dtStart") & "~" & Eval("strContactP") & "~" & Eval("strAdd") & "~" & Eval("strPhone") & "~" & Eval("intSalary") & "~" & Eval("bitAgree") %>

</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>




I want to use it like below but gets error: -

VB
Private Sub Grd_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Grd.RowDataBound
If Session("poweradmin") = True Then
            Dim btnProcess As New Button
            btnProcess = CType(e.Row.FindControl("btnProcess"), Button)

            btnProcess.CommandArgument = "'<%#Eval("intId") & "~" & Eval("IdECompleted") & "~" & Eval("dtECompletion") & "~" & Eval("strName") & " " & Eval("strCode") & " " & Eval("strAge") & " " & Eval("strGender") & "~" & Eval("dtStart") & "~" & Eval("strContactP") & "~" & Eval("strAdd") & "~" & Eval("strPhone") & "~" & Eval("intSalary") & "~" & Eval("bitAgree") %>' />"

        End If
End Sub
Posted
Updated 23-Dec-11 19:42pm
v5

I think so in this situation set value in code behind with better validation. I don't know which list control you are using let say if you are using GridView then fetch control in rowdatabound event and then set the property accordingly.

Thanks
 
Share this answer
 
Comments
Gopal Krishna Ranjan 24-Dec-11 1:29am    
I want to use it like below but gets error: -

Private Sub Grd_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Grd.RowDataBound
If Session("poweradmin") = True Then
Dim btnProcess As New Button
btnProcess = CType(e.Row.FindControl("btnProcess"), Button)

btnProcess.CommandArgument = "CommandArgument='<%#Eval("intId") & "~" & Eval("IdECompleted") & "~" & Eval("dtECompletion") & "~" & Eval("strName") & " " & Eval("strCode") & " " & Eval("strAge") & " " & Eval("strGender") & "~" & Eval("dtStart") & "~" & Eval("strContactP") & "~" & Eval("strAdd") & "~" & Eval("strPhone") & "~" & Eval("intSalary") & "~" & Eval("bitAgree") %>' />"

End If
End Sub
Parwej Ahamad 24-Dec-11 1:34am    
event : ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs

e contains dataitem property so type cast e as your datarow and then fetch all value then build property string then set it to CommandArgument
Gopal Krishna Ranjan 24-Dec-11 1:38am    
please give an example
Parwej Ahamad 24-Dec-11 1:40am    
which framework version you are using?
Gopal Krishna Ranjan 24-Dec-11 1:46am    
framework 3.5
Try following:

Replace all & by +:

HTML
CommandArgument='<%#Eval("intId") + "~" + Eval("IdECompleted") + "~" + Eval("dtECompletion") + "~" + Eval("strName") + "-" + Eval("strCode") + "-" + Eval("strAge") + "-" + Eval("strGender") + "~" + Eval("dtStart") + "~" + Eval("strContactP") + "~" + Eval("strAdd") + "~" + Eval("strPhone") + "~" + Eval("intSalary") + "~" + Eval("bitAgree") %>' />


UPDATED V-1

http://forums.asp.net/post/3264875.aspx[^]

http://stackoverflow.com/questions/661670/how-to-implement-conditional-formatting-in-a-gridview[^]

http://www.aspsnippets.com/Articles/Conditional-Statements-and-String-Formatting-using-Eval-in-ASP.Net.aspx[^]

http://dotnetblogger.com/post/2009/06/16/Inline-Conditional-Statement-using-Eval%28%29-or-Bind%28%29-to-ShowHide-%28Visibile%29-LinkButton-in-a-GridView.aspx[^]

UPDATED V-2
http://stackoverflow.com/questions/2359790/how-to-use-eval-in-codebehind-to-set-page-title[^]
GridView and CommandArguments
 
Share this answer
 
v5
Comments
Gopal Krishna Ranjan 24-Dec-11 1:02am    
I have to set it conditionally. It does not matter & and +. I am using vb.net so & is used instead of + for concatenation.
Monjurul Habib 24-Dec-11 1:15am    
ok go through my updated links, you will get an idea!!
Gopal Krishna Ranjan 24-Dec-11 1:21am    
I checked all. They used conditionally in .aspx page. Can you tell any other method so that i can use it in code behind page (.vb or .cs).
Thanks!
Monjurul Habib 24-Dec-11 1:31am    
check updated v-2 links
VB
Private Sub Grd_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Grd.RowDataBound
        If Session("poweradmin") = True Then
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim btnProcess As New Button
                btnProcess = CType(e.Row.FindControl("btnProcess"), Button)
                btnProcess.CommandArgument = DataBinder.Eval(e.Row.DataItem, "intId") & "~" & DataBinder.Eval _
                (e.Row.DataItem, "IdECompleted") & "~" & DataBinder.Eval(e.Row.DataItem, "dtECompletion") & "~" & DataBinder.Eval _
                (e.Row.DataItem, "strName") & " " & DataBinder.Eval(e.Row.DataItem, "strCode") & " " & DataBinder.Eval _
                (e.Row.DataItem, "strCurrAge") & " " & DataBinder.Eval(e.Row.DataItem, "strGender") & "~" & DataBinder.Eval _
                (e.Row.DataItem, "dtStart") & "~" & DataBinder.Eval(e.Row.DataItem, "strContactP") & "~" & DataBinder.Eval _
                (e.Row.DataItem, "strAdd") & "~" & DataBinder.Eval(e.Row.DataItem, "strPhone") & "~" & DataBinder.Eval _
                (e.Row.DataItem, "intSalary") & "~" & DataBinder.Eval(e.Row.DataItem, "bitAgree")
             End If
        End If
    End Sub
 
Share this answer
 
Comments
Parwej Ahamad 24-Dec-11 2:33am    
Dear I would say using DataBinder.Eval can be performance issue so I would suggest fetch the value from e.Row.DataItem object.
Gopal Krishna Ranjan 24-Dec-11 3:09am    
ok dear thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900