从GridView单元中的数据库加载Asp.net dropdownlist [英] Asp.net dropdownlist load from database in gridview cell

查看:77
本文介绍了从GridView单元中的数据库加载Asp.net dropdownlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在网格视图中单击编辑链接后出现问题的代码
请好好解决我的问题,我将非常感激,上帝会为此给你结果.
谢谢,并等待您的回复.............





this is code which making problem after click on edit link in grid view
please kindly solve my problem I will be tooooo much thankful and God will give u fruit about this.
Thanks, and Waiting for ur reply......................





<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="GenderDataSource" Runat="server"

           SelectCommand="SELECT [genderId], [gender] FROM [Gender]"





            ConnectionString="<%$ ConnectionStrings:Example_DB_CheckConnectionString %>">
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" Runat="server"

            DataSourceID="SqlDataSource1" DataKeyNames="std_id"

            AutoGenerateColumns="False" AllowPaging="True"

            BorderWidth="1px" BackColor="White"

            CellPadding="3" BorderStyle="None" BorderColor="#CCCCCC">
            <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
            <PagerStyle ForeColor="#000066" HorizontalAlign="Left"

               BackColor="White"></PagerStyle>
            <HeaderStyle ForeColor="White" Font-Bold="True"

               BackColor="#006699"></HeaderStyle>
            <Columns>
                <asp:CommandField ShowEditButton="True"></asp:CommandField>
                <asp:BoundField ReadOnly="True" HeaderText="std_id"

                  InsertVisible="False" DataField="std_id"

                    SortExpression="std_id">
                    <ItemStyle HorizontalAlign="Center"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField HeaderText="std_name"

                  DataField="std_name"

                  SortExpression="std_name"></asp:BoundField>
               <asp:TemplateField SortExpression="gender"

                    HeaderText="gender">
                    <EditItemTemplate>
                        <asp:DropDownList ID="gender" runat="server" DataSourceID="GenderDataSource"  DataTextField="gender"

                   DataValueField="genderId"

                     SelectedValue='<%#Bind("genderId")%>'>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label Runat="server"

                       Text='<%# Bind("gender") %>'

                       ID="Label1"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <SelectedRowStyle ForeColor="White"

                Font-Bold="True" BackColor="#669999"></SelectedRowStyle>
            <RowStyle ForeColor="#000066"></RowStyle>
        </asp:GridView>

        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"

            ConnectionString="<%$ ConnectionStrings:Example_DB_CheckConnectionString2 %>"

            SelectCommand="SELECT student.std_id, student.std_name, student.std_fname, student.std_nic, Gender.gender FROM student INNER JOIN Gender ON student.genderId = Gender.genderId" UpdateCommand="UPDATE       student
SET                std_name =@std_nam, std_fname =@std_fname, std_nic =@std_nic, genderId =@genderId
where std_id=@std_id">
            <UpdateParameters>
                <asp:Parameter Name="std_nam" />
                <asp:Parameter Name="std_fname" />
                <asp:Parameter Name="std_nic" />
                <asp:Parameter Name="genderId" />
                <asp:Parameter Name="std_id" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <br />
        <asp:GridView ID="GridView2" runat="server">
        </asp:GridView>
        <br />
        <br />
        <br />
        <br />
        <br />

    </div>
    </form>
</body>
</html>

推荐答案

ConnectionStrings:Example_DB_CheckConnectionString %> " > < /asp:SqlDataSource > < asp:GridView ID =" Runat 服务器" =" SqlDataSource1" DataKeyNames =" std_id" =" 错误" AllowPaging =" =" 1像素" BackColor =" =" 3" BorderStyle =" BorderColor #CCCCCC" < FooterStyle =" #000066" BackColor >白色" > < /FooterStyle > < PagerStyle =" #000066" Horizo​​ntalAlign 左" BackColor =" < > < HeaderStyle =" 白色" 字体粗体 真" BackColor =" > < > < > < asp:CommandField ShowEditButton =" < > < asp:BoundField 只读 =" HeaderText std_id " InsertVisible 错误" DataField =" std_id" SortExpression="std_id"> < ItemStyle =" Center" < > < /asp:BoundField > <asp:BoundField HeaderText="std_name" DataField="std_name" SortExpression="std_name"></asp:BoundField> <asp:TemplateField SortExpression="gender" HeaderText="gender"> < EditItemTemplate > <asp:DropDownList ID="gender" runat="server" DataSourceID="GenderDataSource" DataTextField="gender" DataValueField="genderId" SelectedValue='<%#Bind("genderId")%>'> < /asp:DropDownList > < /EditItemTemplate > < ItemTemplate > <asp:Label Runat="server" span> 文本 =' <%# Bind("gender") %>' ID="Label1" > < /asp:Label > < /ItemTemplate > < /asp:TemplateField > < /列 > <SelectedRowStyle ForeColor="White" Font-Bold="True" BackColor="#669999"></SelectedRowStyle> < RowStyle =" #000066" < > < /asp:GridView > < br > < asp:SqlDataSource ID =" runat 服务器" ConnectionString="<%
ConnectionStrings:Example_DB_CheckConnectionString %>"> </asp:SqlDataSource> <asp:GridView ID="GridView1" Runat="server" DataSourceID="SqlDataSource1" DataKeyNames="std_id" AutoGenerateColumns="False" AllowPaging="True" BorderWidth="1px" BackColor="White" CellPadding="3" BorderStyle="None" BorderColor="#CCCCCC"> <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle> <PagerStyle ForeColor="#000066" HorizontalAlign="Left" BackColor="White"></PagerStyle> <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#006699"></HeaderStyle> <Columns> <asp:CommandField ShowEditButton="True"></asp:CommandField> <asp:BoundField ReadOnly="True" HeaderText="std_id" InsertVisible="False" DataField="std_id" SortExpression="std_id"> <ItemStyle HorizontalAlign="Center"></ItemStyle> </asp:BoundField> <asp:BoundField HeaderText="std_name" DataField="std_name" SortExpression="std_name"></asp:BoundField> <asp:TemplateField SortExpression="gender" HeaderText="gender"> <EditItemTemplate> <asp:DropDownList ID="gender" runat="server" DataSourceID="GenderDataSource" DataTextField="gender" DataValueField="genderId" SelectedValue='<%#Bind("genderId")%>'> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label Runat="server" Text='<%# Bind("gender") %>' ID="Label1"></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <SelectedRowStyle ForeColor="White" Font-Bold="True" BackColor="#669999"></SelectedRowStyle> <RowStyle ForeColor="#000066"></RowStyle> </asp:GridView> <br /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%


ConnectionStrings:Example_DB_CheckConnectionString2 %>" SelectCommand="SELECT student.std_id, student.std_name, student.std_fname, student.std_nic, Gender.gender FROM student INNER JOIN Gender ON student.genderId = Gender.genderId" UpdateCommand="UPDATE student SET std_name =@std_nam, std_fname =@std_fname, std_nic =@std_nic, genderId =@genderId where std_id=@std_id"> < UpdateParameters > <asp:Parameter Name="std_nam" /> <asp:Parameter Name="std_fname" /> <asp:Parameter Name="std_nic" /> <asp:Parameter Name="genderId" /> <asp:Parameter Name="std_id" /> < /UpdateParameters > < /asp:SqlDataSource > < br > <asp:GridView ID="GridView2" runat="server"> < /asp:GridView > < br > < br > < br > < br > < br > < /div > < /form > < /body > </html>
ConnectionStrings:Example_DB_CheckConnectionString2 %>" SelectCommand="SELECT student.std_id, student.std_name, student.std_fname, student.std_nic, Gender.gender FROM student INNER JOIN Gender ON student.genderId = Gender.genderId" UpdateCommand="UPDATE student SET std_name =@std_nam, std_fname =@std_fname, std_nic =@std_nic, genderId =@genderId where std_id=@std_id"> <UpdateParameters> <asp:Parameter Name="std_nam" /> <asp:Parameter Name="std_fname" /> <asp:Parameter Name="std_nic" /> <asp:Parameter Name="genderId" /> <asp:Parameter Name="std_id" /> </UpdateParameters> </asp:SqlDataSource> <br /> <asp:GridView ID="GridView2" runat="server"> </asp:GridView> <br /> <br /> <br /> <br /> <br /> </div> </form> </body> </html>


I would implement the OnRowDataBound and OnRowEditing events on the gridview. Inside of your gridview declaration you will have to set these. Which would look like OnRowDataBound="GridView1_RowDataBound" . Then for your code behind portion create a fucntion for each of the events like so,
I would implement the OnRowDataBound and OnRowEditing events on the gridview. Inside of your gridview declaration you will have to set these. Which would look like OnRowDataBound="GridView1_RowDataBound" . Then for your code behind portion create a fucntion for each of the events like so,
<pre lang="cs">protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)<br />
{<br />
}</pre><br />





<pre lang="cs">protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)<br />
{<br />
}</pre><br />



Inside of each you will have to do different things depending upon your use case. But, inside of the RowEditing event you will need to set GridView1.EditIndex = e.NewEditIndex;, so that the gridview contains the corresponding index of the row to edit.

Inside of the RowDataBound insert code like,



Inside of each you will have to do different things depending upon your use case. But, inside of the RowEditing event you will need to set GridView1.EditIndex = e.NewEditIndex;, so that the gridview contains the corresponding index of the row to edit.

Inside of the RowDataBound insert code like,

<pre lang="cs">if (e.Row.RowType == DataControlRowType.DataRow)<br />
{<br />
     if (GridView1.EditIndex == e.Row.RowIndex)<br />
     {<br />
         //Bind your data to your drop down list here<br />
         //To obtain the drop down list control if you <br />
         //need it, use the following code<br />
         DropDownList editGenderValue = <br />
                     (DropDownList)e.Row.FindControl("gender");<br />
<br />
<br />
     }<br />
}</pre><br />



This is a very short answer to your question that I hope points you in the right direction. If you need more help with implementing this please let me know. Also, there are a few really good articles on code project about how to implement this type of solution in a gridview.

Best of luck and please let me know if you get stuck on anything.



This is a very short answer to your question that I hope points you in the right direction. If you need more help with implementing this please let me know. Also, there are a few really good articles on code project about how to implement this type of solution in a gridview.

Best of luck and please let me know if you get stuck on anything.


这篇关于从GridView单元中的数据库加载Asp.net dropdownlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆