价值无法从动态下拉中获得 [英] value not getting from dynamically dropdown
本文介绍了价值无法从动态下拉中获得的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的所有人,
我在c#中使用Gridview作为我的网络应用程序。我使用Dyanamically Created Template创建了一个动态下拉。
但是我无法获得存储在DB中的下拉值。
那么,我怎样才能从每一列中获得下拉值。
i我正在使用....
Dear All,
I am using Gridview in c# for my web app. I created a Dynamically Dropdown using Dyanamically Created Template.
but i am not able to get the value of dropdown to store in DB.
So, how can i get the value of dropdown from the each column.
i am using ....
public class AddTemplateToGridView : ITemplate
{
ListItemType _type;
string _colName;
public AddTemplateToGridView(ListItemType type, string colname)
{
_type = type;
_colName = colname;
}
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
switch (_type)
{
case ListItemType.Item:
DropDownList ht = new DropDownList();
ht.ID = "ht"+_colName;
ht.Width = 50;
ht.Items.Add(new ListItem("Select", "Select"));
ht.Items.Add(new ListItem("P", "P"));
ht.Items.Add(new ListItem("A", "A"));
ht.Items.Add(new ListItem("H", "H"));
ht.Items.Add(new ListItem("S", "S"));
ht.Items.Add(new ListItem("L", "L"));
ht.DataBinding += new EventHandler(ht_DataBinding);
container.Controls.Add(ht);
break;
}
}
void ht_DataBinding(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow container = (GridViewRow)ddl.NamingContainer;
}
}
点击按钮:
on button click :
DataTable dt = new DataTable();
DataColumn dcol = new DataColumn(NAME, typeof(System.String));
dt.Columns.Add(dcol);
//Create an ID column for adding to the Datatable
int year = DateTime.Now.Year;
int month = DateTime.Now.Month;
int days = DateTime.DaysInMonth(year, month);
string s = "select name from empl where id=''" + Session["user"].ToString() + "''";
SqlDataAdapter ad = new SqlDataAdapter(s, c.getcon());
DataSet ds = new DataSet();
ad.Fill(ds);
if (ds.Tables[0].Rows.Count != 0)
{
string report_person = "Select name from empl where report_to_whom=''" + ds.Tables[0].Rows[0][0].ToString() + "''";
SqlDataAdapter ad1 = new SqlDataAdapter(report_person, c.getcon());
DataSet ds1 = new DataSet();
ad1.Fill(ds1);
if (ds1.Tables[0].Rows.Count != 0)
{
for (int nIndex = 0; nIndex < ds1.Tables[0].Rows.Count; nIndex++) // FOR CREATE A NUMBER OF ROW
{
DataRow drow = dt.NewRow();
drow[NAME] = ds1.Tables[0].Rows[nIndex][0].ToString();
for (int k = 1; k <= days; k++) // FOR CREATE A COLUMN DYNAMICALLY
{
int month1 = DateTime.Today.Month;
int year1 = DateTime.Today.Year;
string sdate = (k) + "/" + month1 + "/" + year1;
string sdate1 = Convert.ToDateTime(sdate).ToString("MM/dd/yyyy");
//GridView gv = Form.FindControl("myGridView") as GridView;
foreach (GridViewRow item in GridView1.Rows)
{
DropDownList d1 = (DropDownList)item.FindControl("htDay1");
}
//string s1 = "insert into attendance(user_name,attendance_date,flag) values(''" + ds1.Tables[0].Rows[nIndex][0].ToString() + "'',''"+sdate1+"'',''"+ddl.SelectedItem.Value+"'')";
//c.InsertData(s1);
}
}
}
}
here, d+k is containg the column name .
how can i get the value and store in db.
i get null always.
plz help me.
Mitesh
推荐答案
正确的语法
Correct syntax
DropDownList d1 =((DropDownList)(item.Controls[0].Controls[3])).SelectedValue
item.Controls [0]是第一个单元格控件index.Controls [3]我的控件索引
item.Controls[0] is first cell controls index.Controls[3] my control index
这篇关于价值无法从动态下拉中获得的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文