正如标题中所写,我试图从DetailsView中获取文本框值。但是,文本框值不在DetailsView中的Binded数据的内容中,因此当我尝试使用OnCommand方法获取文本时,我收到HttpContext错误。其次,我无法使用其ID来获取值,可能是因为它在DetailsView中。最后,当我将文本框和按钮放在DetailsView之外时,我得到了值,但是如果没有要在DetailsView中显示的数据,则按钮和文本不会显示。
这是我的DetailsView代码:
<asp:DetailsView ID="dvÜrün" runat="server" GridLines="None" CssClass="table table-borderless" AutoGenerateRows="false" OnCommand="dvÜrün_ItemCommand">
<Fields>
<asp:TemplateField Visible="false">
<ItemTemplate>
<%#Eval("ÜrünID") %> TL
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ÜrünAdı" HeaderText="Adı" />
<asp:BoundField DataField="ÜrünKategori" HeaderText="Kategori" />
<asp:BoundField DataField="ÜrünAçıklama" HeaderText="Açıklama" />
<asp:TemplateField HeaderText="Ücret">
<ItemTemplate>
<%#Eval("ÜrünÜcret") %> TL
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
Adet:
<asp:textbox ID="Adet" runat="server" CssClass="text-center" textmode="SingleLine" type="number" min="1" max="20" Text="1"/>
<asp:Button CssClass="pull-right btn btn-success" Text="Sepete Ekle" runat="server" OnClick="sepeteEkle_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
答案 0 :(得分:0)
发布后,我立即找到答案。这是我的cs代码:
public void dvÜrün_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "ürünEkle")
{
string adet = ((TextBox)dvÜrün.FindControl("Adet")).Text;
}
}
这是DetailsView代码:
<asp:DetailsView ID="dvÜrün" runat="server" GridLines="None" CssClass="table table-borderless" AutoGenerateRows="false" OnItemCommand="dvÜrün_ItemCommand">
<Fields>
<asp:TemplateField Visible="false">
<ItemTemplate>
<%#Eval("ÜrünID") %> TL
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ÜrünAdı" HeaderText="Adı" />
<asp:BoundField DataField="ÜrünKategori" HeaderText="Kategori" />
<asp:BoundField DataField="ÜrünAçıklama" HeaderText="Açıklama" />
<asp:TemplateField HeaderText="Ücret">
<ItemTemplate>
<%#Eval("ÜrünÜcret") %> TL
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
Adet:
<asp:textbox id="Adet" runat="server" CssClass="text-center" textmode="SingleLine" type="number" min="1" max="20" Text="1"/>
<asp:Button CssClass="pull-right btn btn-success" Text="Sepete Ekle" runat="server" CommandName="ürünEkle"/>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>