/ /条件付きハイパーリンクフィールドのgridview? ASP.net - asp.net、gridview

グリッドビューの条件付きハイパーリンクフィールド? ASP.net - asp.net、gridview

私は、ハイパーリンクフィールドをグリッドビューに入れましたが、時々、データに応じてクリック可能で、時にはそうでないことを望みます。

アイテムがAまたはBの場合、bibble.aspx?id = 123へのハイパーリンクが必要です。そうでない場合は、プレーンテキストが必要です。

何が最善の方法ですか?私はこれのために別のタイプのフィールドを使用すべきですか?

回答:

回答№1は1

おそらく、テンプレートフィールドとハイパーリンクコントロールを使い、三項演算子でNavigateUrlを決定する方がよいでしょう。


回答№2の場合は3
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Column to check">
<ItemTemplate>
<asp:Label runat="server" ID="lblCrtl" Text="<%# Eval("Name") %>" />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Column Name">

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



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// you may need to do this if you didnt use templatefield;
// string val = e.Row.Cells[<column index>].Text;
// if its templatefield do following;
Label lbl = e.Row.FindControl("lblCrtl") as Label;

Button btn = null;

if (lbl.Text == "Car") // put your own value to check, my case it was Car
{
btn = new Button();
btn.Text = "Test";
e.Row.Cells[1].Controls.Add(btn); // cells<column index that control will be added>
}
}
}

回答№3の場合は0

GridViewのRowDataBoundイベントを処理する必要があります。

この リンク RowDataBoundイベントを使用してGridViewコントロールに表示される前に、データソース内のフィールドの値を変更する方法を示します。