前台:<asp:DropDownList ID="tsdlDropDownList" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="tsdl_TypeName"
DataValueField="tsdl_TypeId" ondatabound="tsdlDropDownList_DataBound">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ZHLbookshop1 %>"
SelectCommand="SELECT * FROM [BooksType]"></asp:SqlDataSource>
<asp:DropDownList ID="tsxlDropDownList" runat="server"
ondatabound="tsxlDropDownList_DataBound" DataSourceID="SqlDataSource2"
DataTextField="tsxl_TypeName" DataValueField="tsxl_TypeId">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ZHLbookshop1 %>"
SelectCommand="SELECT * FROM [BooksType2] WHERE ([tsdl_TypeId] = @tsdl_TypeId)">
<SelectParameters>
<asp:ControlParameter ControlID="tsdlDropDownList" Name="tsdl_TypeId"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>后台: protected void AddButton_Click(object sender, EventArgs e)
{
if (tsdlDropDownList.Text == "" || tsdlDropDownList.Text == null || tsxlDropDownList.Text == "" || tsxlDropDownList.Text == null
|| addition.Text == "" || addition.Text == null)
{
common.ShowMsg(this, "请输入要添加的内容!");
return;
}
BooksType3 b = new BooksType3();
b.tsxxl_TypeName = addition.Text;
b.tsdl_TypeId = int.Parse(tsdlDropDownList.SelectedValue);
b.tsxl_TypeId = int.Parse(tsxlDropDownList.SelectedValue);
if (b.Add() > 0){ common.ShowMsg(this, "成功");
}
}还有一个添加类: public int Add()
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into BooksType3(");
strSql.Append("tsxxl_TypeName,tsxl_TypeId,tsdl_TypeId)");
strSql.Append(" values (");
strSql.Append("@tsxxl_TypeName,@tsxl_TypeId,@tsdl_TypeId)");
strSql.Append(";select @@IDENTITY");
SqlParameter[] parameters = {
new SqlParameter("@tsxxl_TypeName", SqlDbType.VarChar,100),
new SqlParameter("@tsxl_TypeId", SqlDbType.Int,4),
new SqlParameter("@tsdl_TypeId", SqlDbType.Int,4)};
parameters[0].Value = tsxxl_TypeName;
parameters[1].Value = tsxl_TypeId;
parameters[2].Value = tsdl_TypeId; return dbHelper.ExecuteCommand(strSql.ToString(), parameters);
}
DataSourceID="SqlDataSource1" DataTextField="tsdl_TypeName"
DataValueField="tsdl_TypeId" ondatabound="tsdlDropDownList_DataBound">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ZHLbookshop1 %>"
SelectCommand="SELECT * FROM [BooksType]"></asp:SqlDataSource>
<asp:DropDownList ID="tsxlDropDownList" runat="server"
ondatabound="tsxlDropDownList_DataBound" DataSourceID="SqlDataSource2"
DataTextField="tsxl_TypeName" DataValueField="tsxl_TypeId">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ZHLbookshop1 %>"
SelectCommand="SELECT * FROM [BooksType2] WHERE ([tsdl_TypeId] = @tsdl_TypeId)">
<SelectParameters>
<asp:ControlParameter ControlID="tsdlDropDownList" Name="tsdl_TypeId"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>后台: protected void AddButton_Click(object sender, EventArgs e)
{
if (tsdlDropDownList.Text == "" || tsdlDropDownList.Text == null || tsxlDropDownList.Text == "" || tsxlDropDownList.Text == null
|| addition.Text == "" || addition.Text == null)
{
common.ShowMsg(this, "请输入要添加的内容!");
return;
}
BooksType3 b = new BooksType3();
b.tsxxl_TypeName = addition.Text;
b.tsdl_TypeId = int.Parse(tsdlDropDownList.SelectedValue);
b.tsxl_TypeId = int.Parse(tsxlDropDownList.SelectedValue);
if (b.Add() > 0){ common.ShowMsg(this, "成功");
}
}还有一个添加类: public int Add()
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into BooksType3(");
strSql.Append("tsxxl_TypeName,tsxl_TypeId,tsdl_TypeId)");
strSql.Append(" values (");
strSql.Append("@tsxxl_TypeName,@tsxl_TypeId,@tsdl_TypeId)");
strSql.Append(";select @@IDENTITY");
SqlParameter[] parameters = {
new SqlParameter("@tsxxl_TypeName", SqlDbType.VarChar,100),
new SqlParameter("@tsxl_TypeId", SqlDbType.Int,4),
new SqlParameter("@tsdl_TypeId", SqlDbType.Int,4)};
parameters[0].Value = tsxxl_TypeName;
parameters[1].Value = tsxl_TypeId;
parameters[2].Value = tsdl_TypeId; return dbHelper.ExecuteCommand(strSql.ToString(), parameters);
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货