读取模板字段标签值绑定到SQL数据库字段

时间:2011-04-21 14:13:25

标签: c# asp.net gridview

我需要从Label Template字段中获取值,该字段绑定到Student表中的StudentID。我想获取StudentID值并将其插入到不同的表中。 这是我的Gridview和SQLDataSource:

<asp:GridView ID="GridView2" 
                        style="position:absolute; top: 232px; left: 311px;" 
                            AutoGenerateColumns="false" runat="server"
                        DataSourceID="SqlDataSource4">
                        <Columns>
                            <asp:TemplateField>
                            <ItemTemplate >
                            <asp:CheckBox runat="server" ID="AttendanceCheckBox" />
                            </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                            <ItemTemplate>
                            <asp:Label ID="studentIDLabel" Text='<%# Eval("StudentID") %>' runat="server"></asp:Label>
                            </ItemTemplate>
                            </asp:TemplateField>                       
                            <asp:BoundField DataField="Name" HeaderText="Name" />
                        </Columns>
                     </asp:GridView>

<asp:SqlDataSource ID="SqlDataSource4" runat="server"
                        ConnectionString="<%$ ConnectionStrings:RegisterConnectionString %>" 
                        SelectCommand="SELECT [StudentID], [Name] FROM [Student] WHERE CourseID = @CourseID ">                         
                        <SelectParameters>
                            <asp:ControlParameter ControlID="CourseDropDownList" Name="CourseID" 
                                PropertyName="SelectedValue" Type="Int32" />
                        </SelectParameters>
                  </asp:SqlDataSource>

这是我的代码,后面将值插入另一个数据库表:

protected void SaveRegisterButton_Click(object sender, EventArgs e)
    {
            SqlConnection connection;
            SqlCommand command;
            int numRowsAdded;
            int id;


    foreach (GridViewRow row in GridView2.Rows)
    {
        if (((CheckBox)row.FindControl("AttendanceCheckBox")).Checked)
        {
           try
            {
                bool attendance = true;

                // Connecting to the database using the connection string in the web.config file
                connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["RegisterConnectionString"].ConnectionString);

                // Create an INSERT Sql statement
                // To prevent an Sql injection attack, we add parameters with names starting with an @ symbol
                command = new SqlCommand("INSERT INTO Attendance(Present, StudentID, LessonID) VALUES(@Attendance, @StudentID, @LessonID)", connection);

                command.Parameters.AddWithValue("@Attendance", attendance); 

任何人都可以帮助我使用代码将标签模板字段(StudentID)中的值插入到考勤表的StudentID字段中。非常感谢任何帮助,提前感谢!

1 个答案:

答案 0 :(得分:1)

 foreach (GridViewRow row in GridView2.Rows)
{
    if (((CheckBox)row.FindControl("AttendanceCheckBox")).Checked)
    {
Int32 StudentID = Convert.ToInt32(((Label)row.FindControl("studentIDLabel")).Text)
....
....
}