单击按钮时如何添加ID?

时间:2012-11-29 16:07:33

标签: asp.net

我想在点击viewdatesbutton.jpg时添加TID的值,以便将其传递给其他网页。

我该怎么做?

代码:

<asp:repeater id="Repeater1" datasourceid="SqlDataSource1" runat="server">

<itemtemplate>

<div class="content-1g">
    <div class="content-1-1g">
        </div>
    <div class="content-1-2g"><p class="none"><%# Eval("TName")%></p><p><span style="color:#FFFFFF"><%# Eval("TDetails")%></span></p>
        <div class="content-1-2ag">
            <div class="content-1-3g">Duration: <%# Eval("TDuration")%> </div>
            <div class="content-1-3g">Adult: £<%# Eval("TPriceadult")%> </div>
            <div class="content-1-3g">Senior/Student: £<%# Eval("TPricesenior")%> </div>
            <div class="content-1-3g">Child: £<%# Eval("TPricechild")%> </div>
        </div>
        <div class="content-1-2bg"><img alt="" src="/images/viewdatesbutton.jpg" />
        </div>
    </div>
</div>

    <div class="line"><hr class="top" /></div>

</itemtemplate>

</asp:repeater>

    <asp:sqldatasource id="SqlDataSource1"  runat="server" 
        ConnectionString="<%$ ConnectionStrings:ChinatowndbConnString %>" 
        SelectCommand="SELECT TID, TName, TDetails, TDuration, TPriceadult, TPricesenior, TPricechild FROM [Tour] WHERE TEventCategory=2">
 </asp:sqldatasource>

我假设我可以在onclick按钮上有一个功能吗?如何传递TID的值?

此致 茶

编辑:

        <asp:LinkButton ID="lbYear" runat="server" CausesValidation="false" OnCommand="redirectnewpage" CommandArgument='<%#Eval("TourId")%>'>
            <asp:Image ID="imgFolder" runat="server" ImageUrl="/images/viewdatesbutton.jpg" />
        </asp:LinkButton>


protected void redirectnewpage(object sender, CommandEventArgs e)
{
    Response.Redirect("book.aspx?tourid="+ e.CommandArgument.ToString());
}

4 个答案:

答案 0 :(得分:1)

您可以使用<asp:ImageButton>服务器端控件并使用它的CommandArgument属性:

<div class="content-1-2bg">
    <asp:ImageButton runat="server" 
         ImageUrl="/images/viewdatesbutton.jpg" 
         CommandArgument='<%# Eval("TID") %>' 
         OnCommand="ImgButton_OnCommand" />

</div>

然后在TID事件处理程序中获取OnCommand值:

private void ImgButton_OnCommand(object sender, CommandEventArgs e)
{
    var tid = int.Parse(e.CommandArgument.ToString());
}

答案 1 :(得分:0)

您可以将ImageButton添加到模板中,并使用CommandArgument插入TID。 例如:

<asp:ImageButton ImageUrl="~/icon.png" ID="TIDButton" runat="server"  
                 CommandArgument=<%# Eval("TID") %> OnClick="TIDButton_Click" />

然后,在你的代码背后得到它:

    protected void TIDButton_Click(object sender, EventArgs e)
    {
        ImageButton b = sender as ImageButton;
        if (b == null)
          return; 

        string TID = b.CommandArgument;
    }

答案 2 :(得分:0)

<asp:LinkButton ID="lbYear" runat="server" CausesValidation="false" OnCommand="redirectnewpage" CommandArgument='<%#Eval("TourId")%>'>
            <asp:Image ID="imgFolder" runat="server" ImageUrl="/images/viewdatesbutton.jpg" />
        </asp:LinkButton>


protected void redirectnewpage(object sender, CommandEventArgs e)
{
    Response.Redirect("book.aspx?tourid="+ e.CommandArgument.ToString());

答案 3 :(得分:0)

    <asp:LinkButton ID="lbYear" runat="server" CausesValidation="false" OnCommand="redirectnewpage" CommandArgument='<%#"tourId="+Eval("TourId").ToString()+"&TID="+Eval("TID").ToString()%>'>
        <asp:Image ID="imgFolder" runat="server" ImageUrl="/images/viewdatesbutton.jpg" />
    </asp:LinkButton>


protected void redirectnewpage(object sender, CommandEventArgs e)
{
   Response.Redirect("book?"+ e.CommandArgument.ToString());
}