在以下示例中,我无法获得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:
到目前为止我已尝试过:
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;
所有抛出对象引用未设置为对象实例异常。
答案 0 :(得分:2)
像这样更改输入:添加runat="server"
属性
在type = "text"
文件中使用aspx
<input type="text" runat="server" id="Contract" onfocus="setSelectedRow(this)" value= '<%#
Eval("Contract") %>' placeholder="Contract #" class='<%# (string)Eval("Status") == "ON
RENT" ? "textBox" : "textBoxHidden" %>' ></input>
然后在type = "tel"
事件中创建GridRows时将其更改为后面代码中的OnRowCreated
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;
答案 1 :(得分:0)
runat="server"
才能访问html输入控件
服务器端value
属性没有=
符号,并且在呈现的html /
醇>
渲染输入标记:
<input type="tel" id="Contract" onfocus="setSelectedRow(this)" value placeholder="Contract #" class="textBoxHidden">
你需要这样的东西:
<input type="text" id="Contract" runat="server" onfocus="setSelectedRow(this)" value='<%# (string)Eval("Contract") == "" ? "" : Eval("Contract") %>' placeholder="Contract #" class='<%# (string)Eval("Status") == "ON RENT" ? "textBox" : "textBoxHidden" %>' />
如果value
属性仍然遗漏=
,请在渲染的html中签名,然后尝试将其值设置为某个虚拟电话。
value='<%# (string)Eval("Contract") == "" ? "0" : Eval("Contract") %>'