• 締切済み

aspxでIFみたいなことがしたい

aspxファイルで、以下のようにデータリストを使用しています。 <asp:DataList ID="datalistBiographies" runat="server" RepeatDirection="Horizontal" Width="100%"> <ItemTemplate> <p> <a href='BiographyInfo.aspx?id=<%# Eval("BiographyID") %>'>View Biography</a><br /> <a id="lnkProjects" href='<%#Eval("ProjectPicture") %>'>Submitted Projects</a> </p> </ItemTemplate> </asp:DataList> ここで、ProjectPictureの値が空文字列のときにこの<a>タグを表示したくないのですが、ASPのときは以下のようにしましたがASP.Netではどのようにすればいいでしょうか? <% If (Not Len(ProjectPicture) > 0) Then %> <a id="lnkProjects" href='<%#Eval("ProjectPicture") %>'>Submitted Projects</a> <% End If %> 基本的なことですみませんがよろしくお願いします。検索の仕方が悪いのか自分で調べることができませんでした。

みんなの回答

回答No.1

<asp:DataList ID="datalistBiographies" runat="server" RepeatDirection="Horizontal" Width="100%"> <ItemTemplate> <p> <a href='BiographyInfo.aspx?id=<%# Eval("BiographyID") %>'>View Biography</a><br /> <a runat="server" id="lnkProjects" href="">Submitted Projects</a> </p> </ItemTemplate> </asp:DataList> <script runat="server"> Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) If ProjectPicture.Length = 0 Then Me.lnkProjects.Visible = False Else Me.lnkProjects.Href = ProjectPicture End If End Sub </script> ASPXではHTMLとプログラムコードが分離しているので、こんな感じになります。

stoutontap
質問者

お礼

自己解決しました。 DataListのItemDataBoundイベントを以下のように実装しました。 protected void datalistProjects_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.DataItem is DataRowView) { DataRowView row = (DataRowView)e.Item.DataItem; if (row["ProjectPicture"] == null || row["ProjectPicture"].ToString().Length == 0) { WebControl link = (WebControl)e.Item.FindControl("lnkProjects"); link.Visible = false; } } }

stoutontap
質問者

補足

以下のエラーになってしまいました。lnkProjectsがデータリストのItemTemplateに入っているからか、参照できないみたいです。 The name 'lnkProjects' does not exist in the current context.

関連するQ&A

  • gridviewの中のCSSを変更したい

    画面にチェックボックスがあります。 チェックボックスを Trueにした時、gridviewのaタグを有効にし、Falseにした時、無効にしたいと思っています。 vb側のチェックボックスを変更した時に、どのように記述すればよいでしょうか? できなければ、Falseの時に、aタグのclassを空白にしてもいいかなと思っています。 どなたかご教授ください。 <div id="containerData" class="containerData" onscroll="containerHeader.scrollLeft=this.scrollLeft;" runat="server"> <asp:GridView ID="grdList" runat="server" AutoGenerateColumns="False" DataSourceID="sqldsList" CssClass="List" DataKeyNames="NO" SelectionMode="None" CellPadding="0" CellSpacing="0" GridLines="None" > <Columns> <asp:TemplateField HeaderText="番号"> <ItemTemplate> <a class="tooltip" title='<%# Eval("NO") %>'> <custom:textLabel ID="lblNo" CssClass="No detailRow" runat="server" Text='<%# Eval("NO") %>' /> </a> </ItemTemplate> <ItemStyle CssClass="rowStyle"></ItemStyle> </asp:TemplateField> <asp:TemplateField HeaderText="名前"> <ItemTemplate> <a class="tooltip" title='<%# Eval("NAME") %>'> <custom:textLabel ID="lblName" CssClass="Name detailRow" runat="server" Text='<%# Eval("NAME") %>' /> </a> </ItemTemplate> <ItemStyle CssClass="rowStyle"></ItemStyle> </asp:TemplateField> </Columns> </asp:GridView> </ContentTemplate> </div>

  • aspx.csファイルでマウスオーバーを発生させるには?

    <asp:Button id=button1 runat="server" onmouseover="-------" /> とaspxファイルに記述するとマウスオーバー効果を利用することができますが、同じことをaspx.csファイルで記述したいのですが、どの様に記述すれば良いのでしょうか?

  • ASP.NETで質問です。

    ASP.NETで質問です。 <asp:Repeater ID="rp" runat="server" OnItemDataBound="rp_OnItemDataBound"> <ItemTemplate> <asp:Literal ID="ltr_num" runat="server" /> <asp:Literal ID="ltr_name" runat="server" /> <br /> </ItemTemplate> </asp:Repeater> rp_OnItemDataBoundメソッドでltr_num,ltr_nameに値を入れたいのですが、 ltr_num.Text,ltr_name.Textにアクセスできません。 どなたか教えてください。 お願い致します。

  • x.aspxとx.aspx.vbの値の受渡について

    asp.net4で開発を実施しております。 FormViewを使ってデータの登録を行おうとしております。 form1.aspx.vbで設定した変数を、form1.aspx側のsqlにて使いたいと思っているのですが、どのようにすればよいでしょうか? 【form1.aspx.vb】 sub aaa = 1 end sub 【form1.aspx】 : <asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:DB1ConnectionString %>" DeleteCommand="UPDATE Table1 SET [Flg1] = @aaa WHERE [CD] = @CD" <DeleteParameters> <asp:Parameter Name="CD" Type="String" /> <asp:Parameter Name="aaa" Type="String" DefaultValue="<%= aaa %>" /> のように作ってみたのですが、うまくいきません。 どの様にすればよいのかご教示いただければと思います。  よろしくお願い致します。

  • ASP GridViewの高さを固定にするには?

    GridViewで全角10文字がちょうど表示できる幅を設定した場合、 10文字を超えるデータを表示しようとするととセル内で折り返してしまいます。 折り返しせず、10文字だけ表示するにはどうすればいいのでしょうか? <div id="containerData" class="containerData" onscroll="containerHeader.scrollLeft=this.scrollLeft;" runat="server"> <asp:UpdatePanel ID="UpdPlView" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:GridView ID="grdList" runat="server" CellPadding="0" AutoGenerateColumns="False" DataSourceID="sqldsList" ShowHeader="False" AllowPaging="False" ShowFooter="True" GridLines="None"> <Columns> <asp:TemplateField HeaderText=""> <ItemTemplate> <table cellspacing="0" class="detailRow"> <tr> <td style="width:100px;"><asp:Label ID="lbl1" runat="server" Text='<%# Eval("Field1") %>'></asp:Label></td> <td style="width:500px;"><asp:Label ID="lbl2" runat="server" Text='<%# Eval("Field2") %>'></asp:Label></td> </tr> </table> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </ContentTemplate> </asp:UpdatePanel> </div> </div> ***スタイルシート*** .containerData { font-size: 11pt; border: solid 1px black; height: 500px; } .detailRow tr { height:30px; background:white; } .detailRow td { border-bottom: 1px solid black; }

  • GridView でコンボボックスの設定

    再質問をさせていただきます。 gridviewを作成しました。 -------------------------------------------------------------------------------- <asp:GridView ID="grdList" runat="server" DataSourceID="sqldsList"> <Columns> <asp:BoundField DataField="ITEMNO" HeaderText="ITEMNO"></asp:BoundField> <asp:TextBox ID="txtNAME" runat="server" Text='<%# Bind("ITEM_NAME") %>'></asp:TextBox> <ItemTemplate>   <asp:DropDownList CssClass="cnbstr" ID="cnb_KBN" runat="server"     DataSourceID="sqldscnb1" DataTextField="NAME" DataValueField="NO" AutoPostBack="True"></asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> -------------------------------------------------------------------------------- このコンボボックスに値を設定させたいのですが、ITEMNO="01"の時だけ、txtNAMEの値を初期表示したいと思っています。 RowDataBound、RowCreatedで可能かと思ったのですができずに困っています。 どなたかご教授お願いします。 Protected Sub grdList_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdList.RowDataBound   If e.Row.RowType = DataControlRowType.DataRow Then     IF DataBinder.Eval(e.Row.DataItem, "ITEMNO").ToString() = "01" THEN       Dim dlist As DropDownList = DirectCast(e.Row.FindControl("cnb_KBN"), DropDownList)       dlist.SelectedValue = "ITEM_NAME" ←こんな感じのことをしたい     ELSE       設定しない     END IF   End If End Sub

  • FormView から別画面でカレンダーを開き、値を前の画面に戻したい

    以下の環境で開発を行っております。 Windows XP(Professional) Visual Web Developer 2008 Express Edition Internet Explorer 8 【質問】 FormView ではなくて Form から別画面でカレンダーを開き値を戻す事はできたのですが、FormView を使った画面からデータを戻すことができず、エラーがでてしまいます。構文が間違っているのでしょうか? 【test.aspx】 <html> <head> <title>日付テスト</title> <script language="javascript" type="text/javascript"> function calendarPicker(strField) { window.open('DatePicker.aspx?field=' + strField, 'calendarPopup', 'width=250,height=220,resizable=yes'); } </script> </head> <body> <form id="Form1" method="post" runat="server"> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStrings %>"> </asp:SqlDataSource> <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1"> <ItemTemplate> 日付:<asp:TextBox ID="TextBox" runat="server" style="text-align: center"></asp:TextBox> <a href="javascript:;" onclick="calendarPicker('Form1.FormView1.TextBox');">▼</a><br /> </ItemTemplate> </asp:FormView> </form> </body> </html> 【DatePicker.aspx.vb】 Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender e.Cell.Controls.Clear() Dim Link As System.Web.UI.HtmlControls.HtmlGenericControl Link = New System.Web.UI.HtmlControls.HtmlGenericControl Link.TagName = "a" Link.InnerText = e.Day.DayNumberText Link.Attributes.Add("href", String.Format("JavaScript:window.opener.document.{0}.value = '{1:d}'; window.close();", Request.QueryString("field"), e.Day.Date)) If e.Day.IsSelected Then Link.Attributes.Add("style",Me.Calendar1.SelectedDayStyle.ToString()) End If e.Cell.Controls.Add(Link) End Sub

  • asp:Contentタグに別のaspsを読み込む事は?

    VWD2008で.NET3.5を使っています。 testA.aspxファイルに以下の様な事を実現したいのですが、可能でしょうか? <asp:Content ID="Content1" ontentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> testB.aspxの中身 </asp:Content> <asp:Content ID="Content2" ontentPlaceHolderID="ContentPlaceHolder2" Runat="Server"> testC.aspxの中身 </asp:Content>

  • DataGridの列幅を実行時に変更したい

    Web画面のSpreadからDataGridへ変更する対応を行っていて、 Spreadの列幅と同じ値を以下のようにテンプレート列を使用して、 ボタンやらチェックボックスやらラベルやら配置をして設定しました。 (TemplateColumnのWidthを使用するとSpreadと同じ値をいれても1/4くらいになってしまったので。) <Columns>  <asp:TemplateColumn>   <ItemTemplate>    <asp:Button id="grdButton" runat="server" Width="45"></asp:Button>   </ItemTemplate>  </asp:TemplateColumn>  <asp:TemplateColumn>   <ItemTemplate>    <asp:Button id="grdButton1" runat="server" Width="45"></asp:Button>   </ItemTemplate>  </asp:TemplateColumn> :          : しかし、実行時に条件によって見える列が変化するため 列を非表示にしたいのですが、Visibled = Falseにしてしまうと、 JavaScriptで、隠している列の値がとれないので、Width = 0にしたいのですが、 grd.Columns(col_2).HeaderStyle.Width = New Unit(0) grd.Columns(col_2).ItemStyle.Width = New Unit(0) というように記述しても列幅が0になっていません。 上記のように実現したければ、どのような手段があるでしょうか?

  • FormViewのEditItemTemplateに配置したボタンへのjavascript組み込み

    はじめまして セバといいます。 VBでaspxを利用しシステムを開発していますが、 次のような現象が発生し困っています。 仕様 1.FormviewのItemTemplateに配置している「edit_Chage」ボタンで FormviewをEditmodeに変更します。 2.FormviewのEditItemTemplateには「hello」ボタンを配置し、 ボタン押下時にjavascriptを起動します。 問題点 FormviewのFormView1_ModeChangedにてイベントを拾い、 「hello」ボタンにjavascriptを組み込もうとしていますが、 この時点では「hello」ボタンのインスタンスが生成おらず、 『オブジェクト参照がオブジェクト インスタンスに設定されていません。』 のエラーになってしまいます。 サンプルプログラム /////////////////////////////// Default.aspx <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "​http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">​ <html xmlns="​http://www.w3.org/1999/xhtml">​ <head runat="server"> <title>無題のページ</title> </head> <body> <form id="form1" runat="server"> <div> </div> <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1"> <EditItemTemplate> tmp1: <asp:TextBox ID="tmp1TextBox" runat="server" Text='<%# Bind("tmp1") %>' /> <asp:Button ID="Button1" runat="server" Text="hello" /> <br /> <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="更新" /> &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="キャンセル" /> </EditItemTemplate> <InsertItemTemplate> </InsertItemTemplate> <ItemTemplate> tmp1: <asp:Label ID="tmp1Label" runat="server" Text='<%# Bind("tmp1") %>' /> <br /> <asp:Button ID="Button2" runat="server" CommandName="edit" Text="edit_Chage" /> </ItemTemplate> </asp:FormView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT [tmp1] FROM [tmpTBL]"> </asp:SqlDataSource> </form> </body> </html> /////////////////////////////// Default.aspx.vb Partial Class _Default Inherits System.Web.UI.Page Protected Sub FormView1_ModeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.ModeChanged '「Hello」ボタンへのjavascript組み込み If FormView1.CurrentMode = FormViewMode.Edit Then Dim btn As Button btn = CType(FormView1.FindControl("Button1"), Button) Dim sbScript As New System.Text.StringBuilder() sbScript.Append("alert('Hello')") btn.Attributes("onclick") = sbScript.ToString() End If End Sub End Class //////////////////////// また、 「Hello」ボタンのインスタンスを強制的に作ろうと、 Default.aspx.vbにFormView1.DataBind()を組み込みましたが、 インスタンスは生成されたものの、 onclickタグは生成されませんでした。 If FormView1.CurrentMode = FormViewMode.Edit Then Dim btn As Button FormView1.DataBind() <--- ここに追加 btn = CType(FormView1.FindControl("Button1"), Button) EditItemTemplateに配置したボタンにプログラムでjavascriptを組み込みたいのですが、 どなたか解決方法をご存知な方がいましたら、ご教示願いします。

専門家に質問してみよう