|
|
|
ASP.Net DropDownList ไม่แสดง row ถัดไป เดาน่าจะเกี่ยวกับ Index ครับ |
|
|
|
|
|
|
|
Dropdownlist combo ไม่แสดง row ถัดไป เดาน่าจะเกี่ยวกับ Index แต่หาไม่เจอครับ ช่วยทีนะครับ งง TTCode (C#)
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Creat_Cliam_From_A_Add_labour_Quatation : System.Web.UI.Page
{
// int rowIndex = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
BindDropDownList();
}
}
private void SetInitialRow()
{
// BindDropDownList();
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
dt.Columns.Add(new DataColumn("Column5", typeof(string)));
dt.Columns.Add(new DataColumn("Column6", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dr["Column5"] = string.Empty;
dr["Column6"] = string.Empty;
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}//end SetInitialRow
private void addNewRowToGrid()
{
SqlConnection con = new SqlConnection("Data Source=OMG-NB;Initial Catalog=DB_Modified;Integrated Security=True");
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
DropDownList drop2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList2");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");
TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox6");
try
{
Response.Write(drop2);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
// Response.Write(drop2.Text);
cmd.CommandText = "INSERT INTO [dbo].[Labour]([LaborCode],[Work code],[LaborName],[ModelCar],[LabourHour],[Group])VALUES('" + drop2.SelectedIndex+ "','" + box2.Text + "','" + box3.Text + "','" + box4.Text + "','" + box5.Text + "','" + box6.Text + "')";
cmd.ExecuteNonQuery();
con.Close();
}
catch
{
}
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Column1"] = drop2.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;
dtCurrentTable.Rows[i - 1]["Column5"] = box5.Text;
dtCurrentTable.Rows[i - 1]["Column6"] = box6.Text;
rowIndex++;
}//end for
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}//end if
}//end if
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}//end addNewRowToGrid
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList drop2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList2");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");
TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox6");
drop2.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();
box4.Text = dt.Rows[i]["Column4"].ToString();
box5.Text = dt.Rows[i]["Column5"].ToString();
box6.Text = dt.Rows[i]["Column6"].ToString();
rowIndex++;
}
//BindDropDownList();
}
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
addNewRowToGrid();
BindDropDownList();
}
protected void Button7_Click(object sender, EventArgs e)
{
OpenNewWindow("result_labour_search.aspx");
}//end med
public void OpenNewWindow(string url)
{
ClientScript.RegisterStartupScript(this.GetType(), "newWindow", String.Format("<script>window.open('{0}');</script>", url));
}
private void BindDropDownList()
{
int rowIndexa = 0;
// DropDownLrowIndexaist ddlCountries = Gridview1.FooterRow.FindControl("ddlCountries") as DropDownList;
// TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
SqlConnection connection = new SqlConnection("Data Source=OMG-NB;Initial Catalog=DB_Modified;Integrated Security=True");
try
{
DataTable dt = new DataTable();
string LaborCode = string.Empty;
string Workcodee = string.Empty;
string ModelCar = string.Empty;
string Equipment = string.Empty;
string newName = string.Empty;
connection.Open();
string sqlStatement = "SELECT * FROM Labour";
SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
int a = 0;
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i <= dt.Rows.Count; i++)
{
LaborCode = dt.Rows[i]["LaborCode"].ToString();
Workcodee = dt.Rows[i]["Work code"].ToString();
ModelCar = dt.Rows[i]["ModelCar"].ToString();
Equipment = dt.Rows[i]["Equipment"].ToString();
newName = LaborCode + " ---- " + Workcodee + " | " + ModelCar + " | " + Equipment;
DropDownList drop2 = (DropDownList)Gridview1.Rows[rowIndexa].Cells[1].FindControl("DropDownList2");
drop2.Items.Add(new ListItem(newName, LaborCode));
//
}
rowIndexa++;
}
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
throw new Exception(msg);
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
// connection.Close();
}
}
}//end class
}
Tag : ASP, Web (ASP.NET), C#, VS 2012 (.NET 4.x), Windows
|
|
|
|
|
|
Date :
2015-02-24 17:49:11 |
By :
tongiggabite |
View :
826 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|