2
我有一個GridView,其中包含手動定義的列。我有幾行(我用一個按鈕來添加一行)。我的問題是同一列中的所有控件都具有相同的ID,因此我無法使用JQuery Datepicker作爲我的日期列(稱爲Fecha)。如何爲每個gridview行中的每個控件設置不同的ID
我相信我可以通過控件ID(如txtFecha1,txtFecha2等)獲得添加一行按鈕來工作(保留舊數據)。但是我應該在哪裏設置這些名稱?
我應該提到,我也會解決一個方法來使JQuery datepicker在同一個id的控件上工作,但很多答案表明它不會工作,因爲datepicker認爲我是一個好的程序員,而我爲每個控件設置一個不同的ID。
代碼爲GridView:
<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>
代碼的添加行按鈕(方法按下按鈕時調用):
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();
}
爲什麼不簡單地添加一個屬性或爲您的html元素分配一個類? –
使用'ClientIDMode ='可預測''_instead of_' Static' – mshsayem
它們有一個類datepicker,像這樣設置:'class =「form-control datepicker」'。我的問題與SO中的許多問題一致,即JQuery datepicker爲任何txtFecha打開,但它總是修改第一行上的控件,因爲它是第一次遇到該id,無論該函數來自哪一行調用。 – Blueriver