我在下面有这个代码,可以为产品列表创建行和单元格。我想使用一个按钮来获取其他产品信息。基本上用户将搜索某些内容,它将显示有限的结果。当用户点击按钮时,它将调用一个可以执行其他操作的方法。
如何将该按钮传递给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);
}
}
}
答案 0 :(得分: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
}
答案 1 :(得分:0)
'<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
}'