gpt4 book ai didi

c# - 在 GridView 的 TemplateField 中获取 Html 输入的值

转载 作者:行者123 更新时间:2023-11-30 17:12:07 25 4
gpt4 key购买 nike

在下面的示例中,我无法获取 tel 输入“Contract”的值。我对 DropDownList 没有任何问题。

aspx代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Serial,Model" DataSourceID="ObjectDataSource1" Width="50%" GridLines="None" CellSpacing="-1">
<Columns>
<asp:BoundField DataField="Serial" HeaderText="Serial" />
<asp:BoundField DataField="Model" HeaderText="Model"/>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="Status" CssClass="dropdownlist" runat="server" onfocus="setSelectedRow(this)" DataValueField="Status" SelectedValue='<%# Bind("Status") %>' onChange="statusChange(this)">
<asp:ListItem Value="RENT READY">Rent Ready</asp:ListItem>
<asp:ListItem Value="ON RENT">On Rent</asp:ListItem>
<asp:ListItem Value="OOS">Out of Service</asp:ListItem>
</asp:DropDownList>
<input type="tel" id="Contract" onfocus="setSelectedRow(this)" value= '<%# Eval("Contract") %>' placeholder="Contract #" class='<%# (string)Eval("Status") == "ON RENT" ? "textBox" : "textBoxHidden" %>'></input>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>

呈现的 HTML:

enter image description here

到目前为止我尝试了什么:

protected void submit_Click(object sender, EventArgs e)
{
if (!Page.IsValid) return;
foreach(GridViewRow row in GridView1.Rows)
{
string Serial = row.Cells[0].Text;
string Model = row.Cells[1].Text;
string Status = ((DropDownList)row.FindControl("Status")).SelectedValue;
string Contract = Any one of the below attempts...

Sql stored procedure integrating the data back to the server...
}
}

string Contract = ((HtmlInputControl)row.FindControl("Contract")).Value;

string Contract = ((HtmlInputText)row.FindControl("Contract")).Value;

string Contract = ((TextBox)row.FindControl("Contract")).Text;

所有抛出 Object Reference Not Set to Instance of Object 异常。

最佳答案

像这样更改您的输入:添加一个 runat="server" 属性

aspx文件中使用type = "text"

 <input type="text" runat="server" id="Contract" onfocus="setSelectedRow(this)" value= '<%#   
Eval("Contract") %>' placeholder="Contract #" class='<%# (string)Eval("Status") == "ON
RENT" ? "textBox" : "textBoxHidden" %>' ></input>

然后像这样在 OnRowCreated 事件中创建 GridRows 时,在后面的代码中将其更改为 type = "tel"

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs {
if (e.Row.RowType == DataControlRowType.DataRow)
((HtmlInputText)(e.Row.FindControl("Contract")))
.Attributes.Add("type", "tel");
}

使用这个检索输入值

string Contract = ((HtmlInputText)row.FindControl("Contract")).Value;

关于c# - 在 GridView 的 TemplateField 中获取 Html 输入的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10983751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com