台灣最大程式設計社群網站
線上人數
1348
 
會員總數:246134
討論主題:189713
歡迎您免費加入會員
討論區列表 >> ASP >> gridview編輯時在dropdownlist選取 checkboxlistt的rows[]設定
[]  
[我要回覆]
1
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
gridview編輯時在dropdownlist選取 checkboxlistt的rows[]設定
價值 : 5 QP  點閱數:772 回應數:1
樓主

123
初學者
115 26
768 59
發送站內信





protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

TextBox name, department, phone, replay, context;
String nameid;
String s = "";
int a;

// ((TextBox)GridView2.Rows[e.RowIndex].FindControl("TextBox1")).Text = ((DropDownList)GridView2.Rows[e.RowIndex].FindControl("DropDownList4")).SelectedItem.ToString();

for (a = 0; a < ((CheckBoxList)GridView2.Rows[e.RowIndex].FindControl("CheckBoxList2")).Items.Count; a++)
{
if (((CheckBoxList)GridView2.Rows[e.RowIndex].FindControl("CheckBoxList2")).Items[a].Selected)
{
s += ((CheckBoxList)GridView2.Rows[e.RowIndex].FindControl("CheckBoxList2")).Items[a].Text + "\n";
((TextBox)GridView2.Rows[e.RowIndex].FindControl("TextBox1")).Text = ((DropDownList)GridView2.Rows[e.RowIndex].FindControl("DropDownList4")).SelectedItem.ToString() + s;
}
}
nameid = ((TextBox)GridView2.Rows[e.RowIndex].Cells[2].FindControl("TextBox1")).Text;
// ((DropDownList)gvStudent.Rows[e.RowIndex].FindControl("DDLSex")).SelectedItem.ToString();
name = (TextBox)GridView2.Rows[e.RowIndex].Cells[3].Controls[0];
department = (TextBox)GridView2.Rows[e.RowIndex].Cells[4].Controls[0];
phone = (TextBox)GridView2.Rows[e.RowIndex].Cells[5].Controls[0];
replay = (TextBox)GridView2.Rows[e.RowIndex].Cells[6].Controls[0];
context = (TextBox)GridView2.Rows[e.RowIndex].Cells[7].Controls[0];

SqlConnection Conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.UpdateCommand = new SqlCommand("update [con1] set [nameid] = @nameid , [name] = @name , [department] = @department, [phone] = @phone , [replay] = @replay, [context] = @context where [number] = @number", Conn);


myAdapter.UpdateCommand.Parameters.Add("@nameid", SqlDbType.VarChar, 500);
myAdapter.UpdateCommand.Parameters["@nameid"].Value = nameid.ToString();

myAdapter.UpdateCommand.Parameters.Add("@name", SqlDbType.VarChar, 500);
myAdapter.UpdateCommand.Parameters["@name"].Value = name.Text;

myAdapter.UpdateCommand.Parameters.Add("@department", SqlDbType.VarChar, 500);
myAdapter.UpdateCommand.Parameters["@department"].Value = department.Text;

myAdapter.UpdateCommand.Parameters.Add("@phone", SqlDbType.VarChar, 500);
myAdapter.UpdateCommand.Parameters["@phone"].Value = phone.Text;

myAdapter.UpdateCommand.Parameters.Add("@replay", SqlDbType.VarChar, 500);
myAdapter.UpdateCommand.Parameters["@replay"].Value = replay.Text;

myAdapter.UpdateCommand.Parameters.Add("@context", SqlDbType.VarChar, 900);
myAdapter.UpdateCommand.Parameters["@context"].Value = context.Text;

myAdapter.UpdateCommand.Parameters.Add("@number", SqlDbType.Int, 4);
myAdapter.UpdateCommand.Parameters["@number"].Value = (int)GridView2.DataKeys[e.RowIndex].Value;

DataSet ds = new DataSet();
myAdapter.SelectCommand = new SqlCommand("select * from con1", Conn);
myAdapter.Fill(ds, "con1");

ds.Tables["con1"].Rows[e.RowIndex]["nameid"] = nameid.ToString();
ds.Tables["con1"].Rows[e.RowIndex]["name"] = name.ToString();
ds.Tables["con1"].Rows[e.RowIndex]["department"] = department.Text;
ds.Tables["con1"].Rows[e.RowIndex]["phone"] = phone.Text;
ds.Tables["con1"].Rows[e.RowIndex]["replay"] = replay.Text;
ds.Tables["con1"].Rows[e.RowIndex]["context"] = context.Text;
myAdapter.Update(ds, "con1");
GridView2.EditIndex = -1;
conn();


}




protected void DropDownList4_SelectedIndexChanged1(object sender, EventArgs e)
{
((TextBox)GridView2.Rows[1].Cells[2].FindControl("TextBox1")).Text = ((DropDownList)GridView2.Rows[1].FindControl("DropDownList4")).SelectedItem.ToString();

((CheckBoxList)GridView2.Rows[1].FindControl("CheckBoxList2")).Items.Clear();
switch (((DropDownList)GridView2.Rows[1].FindControl("DropDownList4")).SelectedValue)
{

case "1":
((CheckBoxList)GridView2.Rows[1].FindControl("CheckBoxList2")).Items.Add(new ListItem("附加配件:電腦主機放置架", "0"));
((CheckBoxList)GridView2.Rows[1].FindControl("CheckBoxList2")).Items.Add(new ListItem("附加配件:延長線", "1"));
((CheckBoxList)GridView2.Rows[1].FindControl("CheckBoxList2")).Items.Add(new ListItem("附加配件:網路線", "2"));
((CheckBoxList)GridView2.Rows[1].FindControl("CheckBoxList2")).Items.Add(new ListItem("附加配件:網路集線器", "3"));
((CheckBoxList)GridView2.Rows[1].FindControl("CheckBoxList2")).Items.Add(new ListItem("其他", "4"));

((CheckBoxList)GridView2.Rows[1].FindControl("CheckBoxList2")).Enabled = true;

break;


case "2":
((CheckBoxList)GridView2.Rows[1].FindControl("CheckBoxList2")).Items.Add(new ListItem("附加配件:電腦主機放置架", "0"));
((CheckBoxList)GridView2.Rows[1].FindControl("CheckBoxList2")).Items.Add(new ListItem("附加配件:延長線", "1"));
((CheckBoxList)GridView2.Rows[1].FindControl("CheckBoxList2")).Items.Add(new ListItem("附加配件:網路線", "2"));
((CheckBoxList)GridView2.Rows[1].FindControl("CheckBoxList2")).Items.Add(new ListItem("附加配件:網路集線器", "3"));
((CheckBoxList)GridView2.Rows[1].FindControl("CheckBoxList2")).Items.Add(new ListItem("其他", "4"));

((CheckBoxList)GridView2.Rows[1].FindControl("CheckBoxList2")).Enabled = true;
break;
}
((CheckBoxList)GridView2.Rows[1]如何寫成跟Rows[e.RowIndex]一樣的效果

搜尋相關Tags的文章: [ (gridview ] , [ dropdownlist ] , [ checkboxlis) ] ,
本篇文章發表於2016-04-19 17:30
1樓
作者回應

123
檢舉此回應
已經想到方法了
本篇文章回覆於2016-06-01 08:45
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
   
1

回覆
如要回應,請先登入.