开发者

DropDropDownList Selected Value

I currently have a dropdownlist which is populated via an SQLDataSource from the Module Table in my database. The Module Table has 3 fields: ModuleID, ModuleName and CourseID. Here is my code for my dropdownlist;

<asp:DropDownList ID="ModuleDropDownList" 
                        style="position:absolute; top: 35px; left: 152px;" runat="server" 
                        DataSourceID="SqlDataSource2" DataTextField="ModuleName" 
                        AutoPostBack="True" 
                        >开发者_如何学运维;

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:RegisterConnectionString %>" 
                        SelectCommand="SELECT DISTINCT * FROM [Module] WHERE ([CourseID] = @CourseID)">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="CourseDropDownList" Name="CourseID" 
                                PropertyName="SelectedValue" Type="Int32" />
                        </SelectParameters>

                    </asp:SqlDataSource>

When the user clicks a Save button on the page then the selected value in the dropdownlist is recorded in another database table called Attendance with a field ModuleID, however the problem I am having is to display all the ModuleName in the dropdownlist but depending on the selected ModuleName it will add the respective ModuleID to the ModuleID field in the Attendance table. Here is my code which saves the selected value in the dropdownlist to the ModuleID field in the Attendance Table;

                command = new SqlCommand("INSERT INTO Attendance(Present, Date, StudentID, StudentName, ModuleID) VALUES (@Attendance, @Date, @StudentID, @StudentName, @ModID)", connection);
                command.Parameters.AddWithValue("@Attendance", attendance);
                command.Parameters.AddWithValue("Date", DateTime.Now);
                command.Parameters.AddWithValue("@StudentID", StudentID);
                command.Parameters.AddWithValue("@StudentName", StudentName);
                command.Parameters.AddWithValue("@ModID", ModuleDropDownList.SelectedValue);

However the ModuleID field in the Attendance table has the data type Int so I need a way of recording the ModuleID in the Attendance table based on the ModuleName chosen from the dropdownlist. Any help is much appreciated, thanks in advance!


based on your code, I'll set the DataValueField property on the "ModuleDropDownList" to "ModuleID"(or whatever you call it). Then you'll get the moduleid when accessing the SelectedValue property of the dropdownlist.

good luck

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜