将gridview的所有dropdownlist值存储到sql表中 [英] Store gridview all dropdownlist value to sql table
本文介绍了将gridview的所有dropdownlist值存储到sql表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的设计页面:
My design Page:
<<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE"
BorderStyle="None" BorderWidth="1px" CellPadding="4"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged" ForeColor="Black"
GridLines="Vertical" onrowdatabound="GridView1_RowDataBound1"
Width="472px">
<Columns>
<asp:TemplateField HeaderText="Encrypt Msg">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Width="200px" TextMode="MultiLine" Text='<%# Eval("Split") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Storage Server">
<ItemTemplate>
<asp:DropDownList ID="Eligible" runat="server" Width="193px">
<asp:ListItem >Storage Server1</asp:ListItem>
<asp:ListItem >Storage Server2</asp:ListItem>
<asp:ListItem >Storage Server3</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<br />
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td style="width: 374px">
<asp:Button ID="upload" runat="server" BorderStyle="None"
onclick="Button1_Click" Text="Click to Upload " />
</td>
<td>
</td>
</tr>
</table>
upload.cs中
:
in upload.cs:
protected void upload_Click(object sender, EventArgs e)
{
for (int j = 0; j < GridView1.Rows.Count; j++)
{
//con.Open();
SqlCommand comm = new SqlCommand("insert into sender '" + GridView1.Rows[j].Cells[0].Text + "','" + GridView1.Rows[j].Cells[1].Text + "'", con);
if (con.State.Equals(ConnectionState.Open))
{
comm.ExecuteNonQuery();
}
}
}
i想要将所有gridview下拉列表值和textbox vlaue插入表...请帮助我...
i want to insert all gridview dropdownlist value and textbox vlaue to table...pls help me...
推荐答案
foreach(GridViewRow gvr in GridView1.Rows)
{
if (gvr.RowType == DataControlRowType.DataRow)
{
string value = ((DropDownList)gvr.FindControl("didDdl")).SelectedValue;
//here write code for inserting in sql table.use parameterized query for preventing from sql injection.
}
}
参见下面的参数化查询示例链接
http:// stackoverflow .com / questions / 12301776 / problems-insertion-to-sql-table [ ^ ]
谢谢
see example of parameterized query in below link
http://stackoverflow.com/questions/12301776/problems-inserting-to-sql-table[^]
Thanks
protected void upload_Click(object sender, EventArgs e)
{
for (int j = 0; j < GridView1.Rows.Count; j++)
{
TextBox TextBox1 =(TextBox)GridView1.Row[j].FindControl("TextBox1 ");
DropDownList Eligible=(DropWownList)GridView1.Row[j].FindControl("Eligible");
//con.Open();
SqlCommand comm = new SqlCommand("insert into sender '" + TextBox1 .Text + "','" + Eligible.Item[Eligible.SelectedIndex].Value + "'", con);
if (con.State.Equals(ConnectionState.Open))
{
comm.ExecuteNonQuery();
}
}
}
这篇关于将gridview的所有dropdownlist值存储到sql表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文