0

我在下面有这段代码,它为产品列表创建行和单元格。我想使用一个按钮来获取其他产品信息。基本上用户会搜索一些东西,它会显示有限的结果。当用户单击按钮时,它将调用一个方法来执行其他操作。

如何让该按钮将 ID 或其他内容传递给方法?

我尝试了.Click但没有调用该方法。目前该方法只显示一个消息框。

for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
{
    // Create new row and add it to the table.
    TableRow tRow = new TableRow();
    Table1.Rows.Add(tRow);
    for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
    {
        // Create a new cell and add it to the row.
        TableCell tCell = new TableCell();

        if (rowCtr == 1 && cellCtr == 1)
        {
            Image ProductImage = new Image();
            ProductImage.Height = 75;
            ProductImage.Width = 75;
            tCell.RowSpan = 5;
            tCell.Controls.Add(ProductImage);
            tRow.Cells.Add(tCell);
        }

        if (rowCtr == 1 && cellCtr == 2)
        {
            tCell.Text = "Title: Title of Product";
            tRow.Cells.Add(tCell);
        }

        if (rowCtr == 2 && cellCtr == 2)
        {
            tCell.Text = "Weight (lbs): 54";
            tRow.Cells.Add(tCell);
        }

        if (rowCtr == 4 && cellCtr == 2)
        {
            Button getOfferButton = new Button();
            getOfferButton.Width = 100;
            getOfferButton.Text = "Get Offer";
            getOfferButton.Click += new EventHandler(getOffer);
            tCell.Controls.Add(getOfferButton);

            tRow.Cells.Add(tCell);
        }

    }
}
4

2 回答 2

1

我认为您应该使用<asp:GridView />控件而不是使用标记生成它。处理此问题的一种方法是使用OnCommand事件并将 ID 作为参数传递

getOfferButton.Click += new CommandEventHandler(RowButton_OnCommand);
getOfferButton.CommandArgument = "123";

然后是处理程序

protected void RowButton_OnCommand(object sender, CommandEventArgs e)
{
   string id = e.CommandArgument.ToString(); //could convert this to integer

}
于 2012-11-03T02:22:35.880 回答
0

首先,您需要从后面的代码中返回对象列表,其次您需要导入您的 BLL 或 BO 名称:

 '<asp:Repeater ID="rptGrpAcc" runat="server" OnItemDataBound="rptAccident_ItemDataBound">
                    <ItemTemplate>
                        <tr style="cursor: pointer" onclick="SelectGrpAcc(this,<%#Eval("ID"%>);">
              </ItemTemplate>
  </asp:Repeter>'

     <asp:HiddenField ID="hdnRowNum" runat="server" />
           <asp:Button ID="btnShowRptDtl" runat="server" Text="ShowRptDtl" Style="display: none"
             OnClick="btnShowRptDtl_Click" CausesValidation="false"/>

      ' <script type="text/javascript">
         /*---------------for selecting row of repeator--------*/
          function SelectGrpAcc(obj, ID) {
        
          document.getElementById("<%=hdnRowNum.ClientID %>").value = ID;
         document.getElementById("<%=btnShowRptDtl.ClientID %>").click();
    }'


'protected void btnShowRptDtl_Click(object sender, EventArgs e)
       {
           btnAdd.Text= "update";
      
          int Sn = ClsConvertTo.Int32(hdnRowNum.Value); // this is your ID in code behind 
       //   add logic here
       }'
于 2014-11-21T11:03:23.940 回答