如何获得网格下拉值 [英] how to get grid dropdown value

查看:79
本文介绍了如何获得网格下拉值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得变量中的网格下拉值。我在这里填写了gridview。



我的代码

  <   asp:GridView     ID   =  gvUserInfo    runat   =  server    AutoGenerateColumns   =  false    OnRowDataBound   =  gvAppraisalForm_RowDataBound      CellSpacing   =  10 >  
< HeaderStyle BackColor = #df5015 字体粗体 = true ForeColor = 白色 / >
< >
< asp:BoundField DataField = sName / >
< asp:TemplateField HeaderText = >
< ItemTemplate >
< asp:DropDownList ID =< /跨度> < span class =code-keyword> ddlCharacteristics runat = server 宽度 = 200px / >
< / ItemTemplate >
< / asp:TemplateField >
< /列 >
< ; / asp:GridView >







protected void BindGridview()

{

SqlCommand cmd = new SqlCommand(select lAppraisalCharacteristicsID,sName来自tblAppraisalCharistics,oSQL._SQLConnection);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds);

gvUserInfo.DataSource = ds;

gvUserInfo.DataBind();

}







protected void gvAppraisalForm_RowDataBound(object sender,GridViewRowEventArgs e)

{

if(e.Row.RowType == DataControlRowType.DataRow)

{



var ddl =(DropDownList) e.Row.FindControl(ddlCharacteristics);

// int CountryId = Convert.ToInt32(e.Row.Cells [0] .Text);

SqlCommand cmd = new SqlCommand(select * from tblAppraisalGrade,oSQL._SQLConnection);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds);

ddl.DataSource = ds;

ddl.DataTextField =sDescription;

ddl.DataValueField =nScore;

ddl.DataBind();

ddl.Items.Insert(0,new ListItem(N-NON Evaluation,0));

}

}

解决方案

您需要列出以下列出的作品:
1.将你的gridview数据绑定(例如BindGridview())放入一个(!IsPostBack)
2.将你的内容gridview,button,label等放在updatepanel内可更新
3.使用触发器对于你即将获得下拉列表值的事件
4.使用foreach循环遍历每一行以获得下拉列表选择值

为您提供帮助我为您提供样本。 :)





 IN .ASPX 





%@ Page Title =主页语言=C#MasterPageFile =〜/ Site.masterAutoEventWireup =true
CodeBehind =Default.aspx.csInherits =WebApplication6._Default%>

< asp:content id = HeaderContent runat = server contentplaceholderid = HeadContent xmlns: asp = #unknown >
< / asp:content >
< a sp:content id = BodyContent runat = 服务器 contentplaceholderid = MainContent xmlns:asp = #unknown >
< asp:updatepanel >
< contenttemplate >
< asp:gridview id = gvUserInfo runat = server < span class =code-attribute> autogeneratecolumns = false onrowdatabound = gvAppraisalForm_RowDataBound onrowcommand = gvAppraisalForm_RowCommand CELLSPACING = 10 >
< HeaderStyle < span class =code-attribute> BackColor
= #df5015 Font-Bold = true ForeColor = 白色 / >
< >
< asp:boundfield datafield = sName / >
< asp:templatefield headertext = >
< itemtemplate >
< asp:dropdownlist id = ddlCharacteristics < span class =code-attribute> runat = server width = 200px / >
< / itemtemplate >
< / asp :templatefield >
< / columns >
< / asp:gridview < span class =code-keyword>>
< asp:label id = lblValues runat = 服务器 > < / asp:label >
< asp:按钮 id = b tnGetValueOfDdl text = 获取值 runat = 服务器 onclick = btnGetValueOfDdl_Click / >
< ; / contenttemplate >
< 触发器 >
< asp:asyncpostbacktrigger controlid = btnGetValueOfDdl eventname < span class =code-keyword> = 点击 / >
< /触发器 >
< / asp:updatepanel >

< / asp:content >





在.cs(代码背后)



< pre lang =c#> 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

命名空间 WebApplication6
{
public partial class _Default:System.Web.UI.Page
{
// 这不适合你我使用它,因为我的数据库中没有像你这样的数据
List< ddlclass> ; lstddlClass = new List< ddlclass>();
public class ddlClass
{
public string sDescription { get ; set ; }
public string sName { get ; set ; }
public int nScore { get ; set ; }
}

// end


受保护 void Page_Load( object sender,EventArgs e)
{

if (!IsPostBack)
{
// 使用您的BindGridview()方法代替此代码块
ddlClass addlclass = < span class =code-keyword> new
ddlClass();
addlclass.sName = Name1;
addlclass.sDescription = description1;
addlclass.nScore = 10 ;
lstddlClass.Add(addlclass);

addlclass = new ddlClass();
addlclass.sName = Name2;
addlclass.sDescription = description2;
addlclass.nScore = 20 ;
lstddlClass.Add(addlclass);

addlclass = new ddlClass();
addlclass.sName = Name3;
addlclass.sDescription = description3;
addlclass.nScore = 30 ;
lstddlClass.Add(addlclass);

gvUserInfo.DataSource = lstddlClass;
gvUserInfo.DataBind();

// end
}

}

protected void gvAppraisalForm_RowDataBound(对象发​​件人,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

var ddl =(DropDownList)e.Row.FindControl( ddlCharacteristics);
// 我评论出你需要打开它的这些代码行

// int CountryId = Convert.ToInt32(e.Row.Cells [0] .Text);
// SqlCommand cmd = new SqlCommand(select * from tblAppraisalGrade,oSQL._SQLConnection);
// SqlDataAdapter da = new SqlDataAdapter(cmd);
// DataSet ds = new DataSet();
// da.Fill(ds);

// end

ddl.DataSource = lstddlClass;
ddl.DataTextField = sDescription;
ddl.DataValueField = nScore;
ddl.DataBind();
ddl.Items.Insert( 0 new ListItem( N-NON Evaluation 0));
}
}



受保护 void gvAppraisalForm_RowCommand( object sender,GridViewCommandEventArgs e)
{



}

受保护 void btnGetValueOfDdl_Click(对象发​​件人,EventArgs e)
{
foreach (GridViewRow row in gvUserInfo.Rows)
{
DropDownList ddl =(DropDownList)row.FindControl( ddlCharacteristics);

lblValues.Text = lblValues.Text + ddl.SelectedValue;
}
}
}
}


how to get grid dropdown value in variabe. i am populated gridview here.

my code

<asp:GridView ID="gvUserInfo" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvAppraisalForm_RowDataBound"  CellSpacing="10">
                    <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                    <Columns>
                        <asp:BoundField DataField="sName"/>
                        <asp:TemplateField HeaderText="">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlCharacteristics" runat="server" Width="200px" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>




protected void BindGridview()
{
SqlCommand cmd = new SqlCommand("select lAppraisalCharacteristicsID,sName from tblAppraisalCharistics", oSQL._SQLConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
}



protected void gvAppraisalForm_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

var ddl = (DropDownList)e.Row.FindControl("ddlCharacteristics");
//int CountryId = Convert.ToInt32(e.Row.Cells[0].Text);
SqlCommand cmd = new SqlCommand("select * from tblAppraisalGrade", oSQL._SQLConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddl.DataSource = ds;
ddl.DataTextField = "sDescription";
ddl.DataValueField = "nScore";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("N-NON Evaluation", "0"));
}
}

解决方案

You need to do a list of works that are listed bellow:
1.Put your gridview databind (e.g. BindGridview()) in inside a (!IsPostBack) 
2.Put your contents gridview,button,label e.t.c that are updateable inside a updatepanel
3.Use trigger for that event in which your are about to get values of dropdownlist
4.use foreach loop to loop through each row to get dropdownlist selected value

For you help I am providing a sample for you . :)



IN .ASPX



%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication6._Default" %>

<asp:content id="HeaderContent" runat="server" contentplaceholderid="HeadContent" xmlns:asp="#unknown">
</asp:content>
<asp:content id="BodyContent" runat="server" contentplaceholderid="MainContent" xmlns:asp="#unknown">
<asp:updatepanel>
<contenttemplate>
<asp:gridview id="gvUserInfo" runat="server" autogeneratecolumns="false" onrowdatabound="gvAppraisalForm_RowDataBound" onrowcommand="gvAppraisalForm_RowCommand" cellspacing="10">
                    <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                    <columns>
                        <asp:boundfield datafield="sName" />
                        <asp:templatefield headertext="">
                            <itemtemplate>
                                <asp:dropdownlist id="ddlCharacteristics" runat="server" width="200px" />
                            </itemtemplate>
                        </asp:templatefield>
                    </columns>
             </asp:gridview>
             <asp:label id="lblValues" runat="server"></asp:label>
              <asp:button id="btnGetValueOfDdl" text="Get Value" runat="server" onclick="btnGetValueOfDdl_Click" />
             </contenttemplate>
             <triggers>
             <asp:asyncpostbacktrigger controlid="btnGetValueOfDdl" eventname="Click" />
             </triggers>
             </asp:updatepanel>
       
</asp:content>



In .cs (Code Behind)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication6
{
    public partial class _Default : System.Web.UI.Page
    {
        // this is not for you I used it as I have no data in Database like yours
        List<ddlclass> lstddlClass = new List<ddlclass>();
        public class ddlClass
        {
            public string sDescription { get; set; }
            public string sName { get; set; }
            public int nScore { get; set; }
        }

        //end


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //use your BindGridview() method instead of this block of code
                ddlClass addlclass = new ddlClass();
                addlclass.sName = "Name1";
                addlclass.sDescription = "description1";
                addlclass.nScore = 10;
                lstddlClass.Add(addlclass);

                addlclass = new ddlClass();
                addlclass.sName = "Name2";
                addlclass.sDescription = "description2";
                addlclass.nScore = 20;
                lstddlClass.Add(addlclass);

                addlclass = new ddlClass();
                addlclass.sName = "Name3";
                addlclass.sDescription = "description3";
                addlclass.nScore = 30;
                lstddlClass.Add(addlclass);

                gvUserInfo.DataSource = lstddlClass;
                gvUserInfo.DataBind();

                //end
            }

        }

        protected void gvAppraisalForm_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                var ddl = (DropDownList)e.Row.FindControl("ddlCharacteristics");
                //I commentout these lines of code you need to open it 

                //int CountryId = Convert.ToInt32(e.Row.Cells[0].Text);
                //SqlCommand cmd = new SqlCommand("select * from tblAppraisalGrade", oSQL._SQLConnection);
                //SqlDataAdapter da = new SqlDataAdapter(cmd);
                //DataSet ds = new DataSet();
                //da.Fill(ds);

                //end

                ddl.DataSource = lstddlClass;
                ddl.DataTextField = "sDescription";
                ddl.DataValueField = "nScore";
                ddl.DataBind();
                ddl.Items.Insert(0, new ListItem("N-NON Evaluation", "0"));
            }
        } 



        protected void gvAppraisalForm_RowCommand(object sender, GridViewCommandEventArgs e)
        {

           
        
        }

        protected void btnGetValueOfDdl_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvUserInfo.Rows)
            {
                DropDownList ddl = (DropDownList)row.FindControl("ddlCharacteristics");

                lblValues.Text =lblValues.Text+ ddl.SelectedValue;
            }
        }
    }
}


这篇关于如何获得网格下拉值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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