asp.net - How to assign a NavigateUrl to a HyperLinkField (GridView) depending on content -
i have gridview boundfields , hyperlinkfields. hyperlinkfield, want assign navigateurl cells have text "reject" in it.
here's markup:
<asp:gridview id="gvs25atransactions" cellspacing="-1" gridlines="none" autogeneratecolumns="false" onprerender="gvs25atransactions_prerender" allowpaging="true" allowsorting="true" pagesize="10" runat="server"> <columns> <asp:boundfield datafield="sin" sortexpression="sin" headertext="<%$ resources:s25atransactions, litsin %>" /> <asp:boundfield datafield="transaction_type_desc_en" sortexpression="transaction_type_desc_en" headertext="<%$ resources:s25atransactions, littype %>" /> <asp:boundfield datafield="transaction_type_desc_fr" sortexpression="transaction_type_desc_fr" headertext="<%$ resources:s25atransactions, littype %>" /> <asp:boundfield datafield="date_updated" sortexpression="date_updated" headertext="<%$ resources:s25atransactions, litdate %>" /> <asp:boundfield datafield="user_code" sortexpression="user_code" headertext="<%$ resources:s25atransactions, lituser %>" /> <asp:hyperlinkfield datatextfield="status_desc_en" sortexpression="status_desc_en" headertext="<%$ resources:s25atransactions, litstatus %>" /> <asp:hyperlinkfield datatextfield="status_desc_fr" sortexpression="status_desc_fr" headertext="<%$ resources:s25atransactions, litstatus %>" /> </columns> <pagerstyle cssclass="pagination" />
and have tried far:
protected sub gvs25atransactions_rowdatabound(sender object, e system.web.ui.webcontrols.gridviewroweventargs) handles gvs25atransactions.rowdatabound if e.row.rowtype = datacontrolrowtype.datarow each column datacontrolfield in gvs25atransactions.columns if column.headertext = "status" or column.headertext = "statut" integer = 0 gvs25atransactions.rows.count - 1 if gvs25atransactions.rows(i).cells(5).text = "rejected sd110" dim hl hyperlink = trycast(e.row.findcontrol("hyperlink1"), hyperlink) hl.navigateurl = "#coucou" end if next end if next end if end sub
the cells text "reject" in either column 5 or 6 (one english, other french).
when code above, never goes in loop.
for integer = 0 gvs25atransactions.rows.count - 1
it says "i" not declared. may inaccessible due it's protection level
help?
update
here's new markup have set in regards reddevil79's suggestions
<asp:gridview id="gvs25atransactions" cellspacing="-1" gridlines="none" autogeneratecolumns="false" onprerender="gvs25atransactions_prerender" allowpaging="true" allowsorting="true" pagesize="10" runat="server"> <columns> <asp:boundfield datafield="sin" sortexpression="sin" headertext="<%$ resources:s25atransactions, litsin %>" /> <asp:boundfield datafield="transaction_type_desc_en" sortexpression="transaction_type_desc_en" headertext="<%$ resources:s25atransactions, littype %>" /> <asp:boundfield datafield="transaction_type_desc_fr" sortexpression="transaction_type_desc_fr" headertext="<%$ resources:s25atransactions, littype %>" /> <asp:boundfield datafield="date_updated" sortexpression="date_updated" headertext="<%$ resources:s25atransactions, litdate %>" /> <asp:boundfield datafield="user_code" sortexpression="user_code" headertext="<%$ resources:s25atransactions, lituser %>" /> <asp:templatefield sortexpression="status_desc_en" headertext="<%$ resources:s25atransactions, litstatus %>"> <itemtemplate> <asp:hyperlink id="status_desc_en" runat="server"><%# eval("status_desc_en")%></asp:hyperlink> </itemtemplate> </asp:templatefield> <asp:templatefield sortexpression="status_desc_fr" headertext="<%$ resources:s25atransactions, litstatus %>"> <itemtemplate> <asp:hyperlink id="status_desc_fr" runat="server"><%# eval("status_desc_fr")%></asp:hyperlink> </itemtemplate> </asp:templatefield> </columns> <pagerstyle cssclass="pagination" />
and vb code
protected sub gvs25atransactions_rowdatabound(sender object, e system.web.ui.webcontrols.gridviewroweventargs) handles gvs25atransactions.rowdatabound if e.row.rowtype = datacontrolrowtype.datarow dim hl hyperlink = trycast(e.row.findcontrol("status_desc_en"), hyperlink) dim hl2 hyperlink = trycast(e.row.findcontrol("status_desc_fr"), hyperlink) if hl.text = "rejected sd110" or hl2.text = "rejeté par sd110" hl.navigateurl = "#coucou" hl2.navigateurl = "#coucoufr" end if end if end sub
the problem facing hl.text , hl2.text ="" never goes in if statement
try this:
convert 2 hyperlink fields in gridview template , alter rowdatabound to:
if e.row.rowtype = datacontrolrowtype.datarow each column datacontrolfield in gvs25atransactions.columns if column.headertext = "status" or column.headertext = "statut" if e.row.cells(5).text = "rejected sd110" dim hl hyperlink = trycast(e.row.findcontrol("hyperlink1"), hyperlink) hl.navigateurl = "#coucou" end if end if next end if
explanation:
you have convert hyperlink fields template because need name of command in findcontrol function.
in rowdatabound don't need iterate through rows of gridview. rowdatabound executed every row in gridview when info added it. that's why don't need loop.
update:
if e.row.rowtype = datacontrolrowtype.datarow dim hl hyperlink = trycast(e.row.findcontrol("status_desc_en"), hyperlink) dim hl2 hyperlink = trycast(e.row.findcontrol("status_desc_fr"), hyperlink) if hl.text = "rejected sd110" or hl2.text = "rejected sd110" hl.navigateurl = "#coucou" end if end if
your hyperlink tags not formed:
<asp:gridview id="gvs25atransactions" cellspacing="-1" gridlines="none" autogeneratecolumns="false" onprerender="gvs25atransactions_prerender" allowpaging="true" allowsorting="true" pagesize="10" runat="server"> <columns> <asp:boundfield datafield="sin" sortexpression="sin" headertext="<%$ resources:s25atransactions, litsin %>" /> <asp:boundfield datafield="transaction_type_desc_en" sortexpression="transaction_type_desc_en" headertext="<%$ resources:s25atransactions, littype %>" /> <asp:boundfield datafield="transaction_type_desc_fr" sortexpression="transaction_type_desc_fr" headertext="<%$ resources:s25atransactions, littype %>" /> <asp:boundfield datafield="date_updated" sortexpression="date_updated" headertext="<%$ resources:s25atransactions, litdate %>" /> <asp:boundfield datafield="user_code" sortexpression="user_code" headertext="<%$ resources:s25atransactions, lituser %>" /> <asp:templatefield sortexpression="status_desc_en" headertext="<%$ resources:s25atransactions, litstatus %>"> <itemtemplate> <asp:hyperlink id="status_desc_en" runat="server" text='<%# eval("status_desc_en")%>'></asp:hyperlink> </itemtemplate> </asp:templatefield> <asp:templatefield sortexpression="status_desc_fr" headertext="<%$ resources:s25atransactions, litstatus %>"> <itemtemplate> <asp:hyperlink id="status_desc_fr" runat="server" text='<%# eval("status_desc_fr")%>'></asp:hyperlink> </itemtemplate> </asp:templatefield> </columns> <pagerstyle cssclass="pagination" /> </asp:gridview>
you have bind field text property of hyperlink.
important: text property works ' not "
asp.net vb.net gridview
No comments:
Post a Comment