如何在列表视图中使用插入参数与文本框文本
本文关键字:文本 插入 参数 列表 视图 | 更新日期: 2023-09-27 18:14:37
我试图使用我的SqlDataSource
从文本框的值输入项目到数据库中,但是没有插入。
这是我的SqlDataSource
:
<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem FROM Core.SectionItem_Lkup">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SectionIDDataSource" runat="server " ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItemID, SectionItem, SectionItemLabel) VALUES (@SectionID, @SectionItem, @SectionItemLabel)" SelectCommand=" SELECT MAX(SectionItemID) + 1 AS SectionItemID FROM [ORHP_Dev03182014].[Core].[SectionItem_Lkup]" OnInserting="Section_OnInserted">
<InsertParameters>
<asp:Parameter Name="SectionID" />
<asp:Parameter Name="SectionItem" />
<asp:Parameter Name="SectionItemLabel" />
</InsertParameters>
</asp:SqlDataSource>
下面是listview中的文本框:
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" On />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
</td>
<td>
<div style="font-size: .8em;">
<asp:FormView ID="SectionIDFormView" runat="server" DataKeyNames="SectionItemID" DataSourceID="SectionIDDataSource">
<ItemTemplate>
<asp:Label ID="SectionIDLabel" runat="server" Text="SectionID" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionIDTextBox" runat="server" Text='<%# Eval("SectionItemID") %>' Width="50px" />
<asp:Label ID="SectionItemLabel" runat="server" Text="SectionItem" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemTextBox" runat="server" Text="" />
<asp:Label ID="SectionItemSubLabel" runat="server" Text="SectionItem Label" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemLabelTextBox" runat="server" Text="" />
</ItemTemplate>
</asp:FormView>
</div>
</td>
</tr>
</InsertItemTemplate>
下面是我的代码:
using (SqlConnection connection = new SqlConnection("Data Source=RCK-HRSA-DB01;Initial Catalog=ORHP_Dev03182014;User ID=userid;Password=password"))
{
try
{
SqlCommand cmd1 = new SqlCommand("INSERT INTO Core.SectionItem_Lkup(SectionItemID, SectionItem, SectionItemLabel) VALUES (" + SectionIDTextBox.Text + ", " + SectionItemTextBox.Text + ", " + SectionItemLabelTextBox.Text + ")");
connection.Open();
cmd1.Connection = connection;
cmd1.CommandType = CommandType.Text;
cmd1.ExecuteNonQuery();
}
catch (Exception ex)
{
}
}
我错过了什么吗?
当你要去的路线是找到你的ControlParameters控件时,你会遇到的问题。当控件位于数据绑定控件中时,ID不是您声明的那样。您可以在渲染后在页面上查看源代码时看到这一点。
对于FormViews,它通常出来的东西像SectionIDFormView$SectionIDLabel
,这显然是不一样的SectionIDLabel
。所以使用这样的ControlParameter可能对你有用:
<InsertParameters>
<asp:ControlParameter ControlID="SectionIDFormView$SectionIDLabel"
Direction="Input" Name="SectionID" PropertyName="Text" />
</InsertParameters>
作为替代方案,当单击ListView上的Insert
按钮时,处理单击事件并在其中执行插入操作。将插入操作从SqlDataSource中移除。
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
ListViewItem item = ListView1.InsertItem;
FormView SectionIDFormView = (FormView)item.FindControl("SectionIDFormView");
TextBox SectionIDTextBox = (TextBox)SectionIDFormView.FindControl("SectionIDTextBox");
//Find your other controls
//Do your insert
}
}
尝试使用参数化查询:
SqlCommand cmd1 = new SqlCommand("INSERT INTO Core.SectionItem_Lkup(SectionItemID, SectionItem, SectionItemLabel) VALUES (@SectionItemID, @SectionItem, @SectionItemLabel)");
cmd1.Parameters.AddWithValue("@SectionItemID", SectionIDTextBox.Text);
cmd1.Parameters.AddWithValue("@SectionItem", SectionItemTextBox.Text);
cmd1.Parameters.AddWithValue("@SectionItemLabel", SectionItemLabelTextBox.Text);