C# 如何为每个gridview行中的每个控件设置不同的ID
我有一个手动定义列的GridView。我有几行(我使用按钮添加一行)。我的问题是,同一列中的所有控件都具有相同的id,因此我不能对我的日期列(称为Fecha)使用JQuery Datepicker 我相信我可以让AddaRow按钮使用控件ID(如txtFecha1、txtFecha2等)工作(保留旧数据)。但是我应该在哪里设置这些名称呢 我应该提到的是,我也会满足于让JQuery datepicker在具有相同id的控件上工作的方法,但许多答案表明它不会工作,因为datepicker认为我是一个优秀的程序员,并且我为每个控件设置了不同的id GridView的代码:C# 如何为每个gridview行中的每个控件设置不同的ID,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个手动定义列的GridView。我有几行(我使用按钮添加一行)。我的问题是,同一列中的所有控件都具有相同的id,因此我不能对我的日期列(称为Fecha)使用JQuery Datepicker 我相信我可以让AddaRow按钮使用控件ID(如txtFecha1、txtFecha2等)工作(保留旧数据)。但是我应该在哪里设置这些名称呢 我应该提到的是,我也会满足于让JQuery datepicker在具有相同id的控件上工作的方法,但许多答案表明它不会工作,因为datepicker认为我是一个
<asp:GridView CssClass="table table-striped table-bordered table-condensed"
ID="gvActividades" runat="server" EmptyDataText="Error" AllowPaging="False"
AutoGenerateColumns="false" OnRowDataBound="gvActividades_OnRowDataBound"
OnRowDeleting="gvActividades_RowDeleting">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField runat="server" HeaderText="Técnico">
<ItemTemplate>
<asp:DropDownList runat="server" ClientIDMode="Static" class="form-control" ID="cboTecnico">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" HeaderText="Fecha">
<ItemTemplate>
<asp:TextBox runat="server" ClientIDMode="Static" class="form-control datepicker" ID="txtFecha" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" HeaderText="Hora de inicio">
<ItemTemplate>
<asp:TextBox runat="server" ClientIDMode="Static" class="form-control" ID="txtHoraInicio" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" HeaderText="Hora de fin">
<ItemTemplate>
<asp:TextBox runat="server" ClientIDMode="Static" class="form-control" ID="txtHoraFin" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" HeaderText="Repuesto">
<ItemTemplate>
<asp:DropDownList runat="server" ClientIDMode="Static" class="form-control" ID="cboRepuesto">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" HeaderText="Cantidad">
<ItemTemplate>
<asp:TextBox runat="server" ClientIDMode="Static" class="form-control" ID="txtCantidad" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" HeaderText="Descripción">
<ItemTemplate>
<asp:TextBox runat="server" ClientIDMode="Static" class="form-control" ID="txtDescripcion" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle CssClass="gvSelectedRowStyle" />
<PagerStyle CssClass="gvPagerStyle" />
</asp:GridView>
“添加行”按钮的代码(按下按钮时调用的方法):
protectedvoid cmdAgregarFila\u单击(对象发送方,事件参数e)
{
如果(null==ViewState[“CurrentTable”])
{
返回;
}
int rowIndex=0;
DataTable dtCurrentTable=(DataTable)视图状态[“CurrentTable”];
DataRow drCurrentRow=null;
如果(dtCurrentTable.Rows.Count>0)
{
对于(int i=1;i 0)
{
对于(int i=0;i1)
{
删除(dt.Rows[rowIndex]);
drCurrentRow=dt.NewRow();
视图状态[“当前表”]=dt;
gvitidades.DataSource=dt;
gvitidades.DataBind();
SetPreviousData();
//总人数();
}
}
}
私有void SetRowData()
{
int rowIndex=0;
如果(null==ViewState[“CurrentTable”])
{
返回;
}
DataTable dtCurrentTable=(DataTable)视图状态[“CurrentTable”];
DataRow drCurrentRow=null;
如果(dtCurrentTable.Rows.Count>0)
{
对于(int i=1;i使用clientdmode=“可预测”
而不是clientdmode=“Static”
在项目模板中。为什么不简单地添加一个属性或为html元素分配一个类呢?使用clientdmode=“可预测”
而不是Static
它们有一个类日期选择器,设置如下:class=“窗体控件日期选择器”"
。我的问题与SO中的许多问题一致,就是当JQuery datepicker为任何txtFecha打开时,它总是修改第一行上的控件,因为这是它第一次遇到该id,无论从哪一行调用该函数。@mshsayem Date,非常简单,工作得很好!!非常感谢!请将它作为一个示例回答这个问题,让其他人容易发现。
protected void cmdAgregarFila_Click(object sender, EventArgs e)
{
if (null == ViewState["CurrentTable"])
{
return;
}
int rowIndex = 0;
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
DropDownList cboTecnico =
(DropDownList)gvActividades.Rows[rowIndex].Cells[1].FindControl("cboTecnico");
TextBox txtFecha =
(TextBox)gvActividades.Rows[rowIndex].Cells[2].FindControl("txtFecha");
TextBox txtHoraInicio =
(TextBox)gvActividades.Rows[rowIndex].Cells[3].FindControl("txtHoraInicio");
TextBox txtHoraFin =
(TextBox)gvActividades.Rows[rowIndex].Cells[4].FindControl("txtHoraFin");
DropDownList cboRepuesto =
(DropDownList)gvActividades.Rows[rowIndex].Cells[5].FindControl("cboRepuesto");
TextBox txtCantidad =
(TextBox)gvActividades.Rows[rowIndex].Cells[6].FindControl("txtCantidad");
TextBox txtDescripcion =
(TextBox)gvActividades.Rows[rowIndex].Cells[7].FindControl("txtDescripcion");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Col1"] = cboTecnico.SelectedValue;
dtCurrentTable.Rows[i - 1]["Col2"] = txtFecha.Text;
dtCurrentTable.Rows[i - 1]["Col3"] = txtHoraInicio.Text;
dtCurrentTable.Rows[i - 1]["Col4"] = txtHoraFin.Text;
dtCurrentTable.Rows[i - 1]["Col5"] = cboRepuesto.SelectedValue;
dtCurrentTable.Rows[i - 1]["Col6"] = txtCantidad.Text;
dtCurrentTable.Rows[i - 1]["Col7"] = txtDescripcion.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
gvActividades.DataSource = dtCurrentTable;
gvActividades.DataBind();
}
SetPreviousData();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FirstGridViewRow();
}
}
private void FirstGridViewRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Col1", typeof(string)));
dt.Columns.Add(new DataColumn("Col2", typeof(string)));
dt.Columns.Add(new DataColumn("Col3", typeof(string)));
dt.Columns.Add(new DataColumn("Col4", typeof(string)));
dt.Columns.Add(new DataColumn("Col5", typeof(string)));
dt.Columns.Add(new DataColumn("Col6", typeof(string)));
dt.Columns.Add(new DataColumn("Col7", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Col1"] = string.Empty;
dr["Col2"] = string.Empty;
dr["Col3"] = string.Empty;
dr["Col4"] = string.Empty;
dr["Col5"] = string.Empty;
dr["Col6"] = string.Empty;
dr["Col7"] = string.Empty;
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
gvActividades.DataSource = dt;
gvActividades.DataBind();
}
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 cboTecnico =
(DropDownList)gvActividades.Rows[rowIndex].Cells[1].FindControl("cboTecnico");
TextBox txtFecha =
(TextBox)gvActividades.Rows[rowIndex].Cells[2].FindControl("txtFecha");
TextBox txtHoraInicio =
(TextBox)gvActividades.Rows[rowIndex].Cells[3].FindControl("txtHoraInicio");
TextBox txtHoraFin =
(TextBox)gvActividades.Rows[rowIndex].Cells[4].FindControl("txtHoraFin");
DropDownList cboRepuesto =
(DropDownList)gvActividades.Rows[rowIndex].Cells[5].FindControl("cboRepuesto");
TextBox txtCantidad =
(TextBox)gvActividades.Rows[rowIndex].Cells[6].FindControl("txtCantidad");
TextBox txtDescripcion =
(TextBox)gvActividades.Rows[rowIndex].Cells[7].FindControl("txtDescripcion");
cboTecnico.SelectedValue = dt.Rows[i]["Col1"].ToString();
txtFecha.Text = dt.Rows[i]["Col2"].ToString();
txtHoraInicio.Text = dt.Rows[i]["Col3"].ToString();
txtHoraFin.Text = dt.Rows[i]["Col4"].ToString();
cboRepuesto.SelectedValue = dt.Rows[i]["Col5"].ToString();
txtCantidad.Text = dt.Rows[i]["Col6"].ToString();
txtDescripcion.Text = dt.Rows[i]["Col7"].ToString();
rowIndex++;
}
}
}
}
protected void gvActividades_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SetRowData();
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
int rowIndex = Convert.ToInt32(e.RowIndex);
if (dt.Rows.Count > 1)
{
dt.Rows.Remove(dt.Rows[rowIndex]);
drCurrentRow = dt.NewRow();
ViewState["CurrentTable"] = dt;
gvActividades.DataSource = dt;
gvActividades.DataBind();
SetPreviousData();
//actualizarTotal();
}
}
}
private void SetRowData()
{
int rowIndex = 0;
if (null == ViewState["CurrentTable"])
{
return;
}
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
DropDownList cboTecnico =
(DropDownList)gvActividades.Rows[rowIndex].Cells[1].FindControl("cboTecnico");
TextBox txtFecha =
(TextBox)gvActividades.Rows[rowIndex].Cells[2].FindControl("txtFecha");
TextBox txtHoraInicio =
(TextBox)gvActividades.Rows[rowIndex].Cells[3].FindControl("txtHoraInicio");
TextBox txtHoraFin =
(TextBox)gvActividades.Rows[rowIndex].Cells[4].FindControl("txtHoraFin");
DropDownList cboRepuesto =
(DropDownList)gvActividades.Rows[rowIndex].Cells[5].FindControl("cboRepuesto");
TextBox txtCantidad =
(TextBox)gvActividades.Rows[rowIndex].Cells[6].FindControl("txtCantidad");
TextBox txtDescripcion =
(TextBox)gvActividades.Rows[rowIndex].Cells[7].FindControl("txtDescripcion");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Col1"] = cboTecnico.SelectedValue;
dtCurrentTable.Rows[i - 1]["Col2"] = txtFecha.Text;
dtCurrentTable.Rows[i - 1]["Col3"] = txtHoraInicio.Text;
dtCurrentTable.Rows[i - 1]["Col4"] = txtHoraFin.Text;
dtCurrentTable.Rows[i - 1]["Col5"] = cboRepuesto.SelectedValue;
dtCurrentTable.Rows[i - 1]["Col6"] = txtCantidad.Text;
dtCurrentTable.Rows[i - 1]["Col7"] = txtDescripcion.Text;
rowIndex++;
}
ViewState["CurrentTable"] = dtCurrentTable;
}
SetPreviousData();
}