
07/01/2008, 21:32
|
 | Colaborador | | Fecha de Ingreso: septiembre-2007 Ubicación: San Francisco, United States
Mensajes: 3.858
Antigüedad: 17 años, 6 meses Puntos: 87 | |
Re: Por Favor Ayuda urgente con un Dropdownlist !!! Aqui hay un pequeño Ejemplo que puedes adaptar a tu necesidad
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Deepra;uid=sa;pwd=;");
public static SqlDataAdapter ad;
SqlCommandBuilder cbuilder;
public static DataSet ds = new DataSet();
static int currentRow=0;
static string opMode="";
protected void ShowData()
{
TextBox1.Text = Convert.ToString(ds.Tables[0].Rows[currentRow].ItemArray[0]);
TextBox2.Text = Convert.ToString(ds.Tables[0].Rows[currentRow].ItemArray[1]);
TextBox3.Text = Convert.ToString(ds.Tables[0].Rows[currentRow].ItemArray[2]);
}
protected void LoadData()
{
ad = new SqlDataAdapter("select * from student", con);
cbuilder = new SqlCommandBuilder(ad);
ad.InsertCommand = cbuilder.GetInsertCommand();
ad.UpdateCommand = cbuilder.GetUpdateCommand();
ad.DeleteCommand = cbuilder.GetDeleteCommand();
ds.Clear();
ad.Fill(ds, "stud");
GridView1.DataSource = ds.Tables["stud"];
GridView1.DataBind();
ShowData();
TextBox1.ReadOnly = true;
TextBox2.ReadOnly = true;
TextBox3.ReadOnly = true;
BtnAdd.Enabled = true;
BtnEdit.Enabled = true;
BTnSave.Enabled = false;
}
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
LoadData();
//TextBox1.Text = (string)DataBinder.Eval(ds,"ds.Tables[0].DefaultView[0].Row[0]");
//TextBox2.Text = (string)DataBinder.Eval(ds.Tables["stud"], "Name");
//TextBox3.Text = (string)DataBinder.Eval(ds.Tables["stud"], "Address");
//Page.DataBind();
}
protected void BtnFirst_Click(object sender, EventArgs e)
{
currentRow = 0;
ShowData();
}
protected void BtnPrevious_Click(object sender, EventArgs e)
{
if (currentRow>0)
{
currentRow--;
ShowData();
}
}
protected void BtnNext_Click(object sender, EventArgs e)
{
if (currentRow < ds.Tables[0].Rows.Count-1)
{
currentRow++;
ShowData();
}
}
protected void BtnLast_Click(object sender, EventArgs e)
{
currentRow = ds.Tables[0].Rows.Count - 1;
ShowData();
}
protected void BtnFind_Click(object sender, EventArgs e)
{
DataRow[] dr=ds.Tables[0].Select("roll=" + Int32.Parse(TxtFind.Text));
if (dr.Length == 0)
{
Response.Write("Record Not Found");
}
else
{
TextBox1.Text = Convert.ToString(dr[0].ItemArray[0]);
TextBox2.Text = Convert.ToString(dr[0].ItemArray[1]);
TextBox3.Text = Convert.ToString(dr[0].ItemArray[2]);
currentRow = ds.Tables[0].Rows.IndexOf(dr[0]);
}
}
protected void BtnAdd_Click(object sender, EventArgs e)
{
opMode = "New";
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox1.ReadOnly = false;
TextBox2.ReadOnly = false;
TextBox3.ReadOnly = false;
BtnAdd.Enabled = false;
BtnEdit.Enabled = false;
BTnSave.Enabled = true;
}
protected void BtnEdit_Click(object sender, EventArgs e)
{
opMode = "Edit";
TextBox1.ReadOnly = true;
TextBox2.ReadOnly = false;
TextBox3.ReadOnly = false;
BtnAdd.Enabled = false;
BtnEdit.Enabled = false;
BTnSave.Enabled = true;
}
protected void BTnSave_Click(object sender, EventArgs e)
{
if (opMode == "New")
{
DataRow dr = ds.Tables[0].NewRow();
dr["roll"] = TextBox1.Text;
dr["Name"] = TextBox2.Text;
dr["Address"] = TextBox3.Text;
ds.Tables[0].Rows.Add(dr);
ad.Update(ds.Tables["stud"]);
Response.Write("Record saved");
currentRow = 0;
LoadData();
}
else if(opMode=="Edit")
{
ds.Tables[0].Rows[currentRow].BeginEdit();
ds.Tables[0].Rows[currentRow]["roll"]=this.TextBox1.Text;
ds.Tables[0].Rows[currentRow]["Name"]=this.TextBox2.Text;
ds.Tables[0].Rows[currentRow]["Address"] = this.TextBox3.Text;
ds.Tables[0].Rows[currentRow].EndEdit();
ad.Update(ds.Tables["stud"]);
Response.Write("Record saved");
currentRow = 0;
LoadData();
}
}
protected void BtnDELETE_Click(object sender, EventArgs e)
{
ds.Tables[0].Rows[currentRow].Delete();
ad.Update(ds.Tables[0]);
Response.Write("REcord DEleted");
currentRow = 0;
LoadData();
}
}
Puedes DEscomentar lineas y Ahorrar Codigo asociandolo al evento de cambio de seleccion de tu DDL |