我想在使用javascript点击datalist item链接按钮时将项目添加到gridview。
这是我尝试的示例,我在C#代码中将其放在下面以供您澄清。
protected void DataList_itemCommand(object source, DataListCommandEventArgs e)
{
lblError.Text = "";
int index = Convert.ToInt32(e.Item.ItemIndex);
DataListItem item = dlOrderList.Items[index];
Label lblProductCode = (Label)item.FindControl("lblProductCode");
Label lblProductName = (Label)item.FindControl("lblProductName");
Label lblUnitCode = (Label)item.FindControl("lblUnitCode");
TextBox txtQty = (TextBox)item.FindControl("txtQty");
Label lblMinQty = (Label)item.FindControl("lblMinQty");
Label lblRate = (Label)item.FindControl("lblRate");
if (txtQty.Text.Trim() == "")
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please enter the Quantity!')", true);
}
else if (txtQty.Text.Trim() == "0")
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please enter the valid Quantity')", true);
}
else if (Convert.ToDouble(txtQty.Text.Trim()) < Convert.ToDouble(lblMinQty.Text))
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Minimum Quantity to Order is " + lblMinQty.Text + "')", true);
}
else
{
Double Amount = (Convert.ToDouble(txtQty.Text.Trim()) * Convert.ToDouble(lblRate.Text));
DataTable dtOrder = new DataTable();
dtOrder.Columns.Add("ProductCode", typeof(string));
dtOrder.Columns.Add("ProductName", typeof(string));
dtOrder.Columns.Add("Qty", typeof(string));
dtOrder.Columns.Add("Rate", typeof(string));
dtOrder.Columns.Add("Amount", typeof(string));
if (Session["CurrentTable"] != null)
{
dtOrder = (DataTable)Session["CurrentTable"];
}
bool Valid = false;
for (int row = 0; row <= dtOrder.Rows.Count - 1; row++)
{
if (lblProductCode.Text == dtOrder.Rows[row]["ProductCode"].ToString())
{
dtOrder.Rows[row]["ProductCode"] = lblProductCode.Text;
dtOrder.Rows[row]["ProductName"] = lblProductName.Text;
if (lblUnitCode.Text.ToString() == "1")
{
dtOrder.Rows[row]["Qty"] = Convert.ToDecimal(txtQty.Text).ToString("#0.000");
}
else
{
dtOrder.Rows[row]["Qty"] = txtQty.Text;
}
dtOrder.Rows[row]["Rate"] = Convert.ToDecimal(lblRate.Text).ToString("#0.00");
dtOrder.Rows[row]["Amount"] = Amount.ToString("N2");
Valid = true;
break;
}
}
if (Valid == false)
{
DataRow dr = dtOrder.NewRow();
dr[0] = lblProductCode.Text;
dr[1] = lblProductName.Text;
if (lblUnitCode.Text.ToString() == "1")
{
dr[2] = Convert.ToDecimal(txtQty.Text).ToString("#0.000");
}
else
{
dr[2] = txtQty.Text;
}
dr[3] = Convert.ToDecimal(lblRate.Text).ToString("#0.00");
dr[4] = Amount.ToString("#0.00");
dtOrder.Rows.Add(dr);
}
gdOrder.DataSource = dtOrder;
Session["CurrentTable"] = dtOrder;
gdOrder.DataBind();
if (dtOrder.Rows.Count == 0)
{
btnOrder.Visible = false;
}
else
{
btnOrder.Visible = true;
}
txtQty.Text = string.Empty;
}
}
protected void gdOrder_Delete(object sender, GridViewDeleteEventArgs e)
{
try
{
if (Session["CurrentTable"] != null)
{
DataTable dt = (DataTable)Session["CurrentTable"];
DataRow drCurrentRow = null;
int rowIndex = Convert.ToInt32(e.RowIndex);
if (dt.Rows.Count > 0)
{
dt.Rows.Remove(dt.Rows[rowIndex]);
drCurrentRow = dt.NewRow();
Session["CurrentTable"] = dt;
gdOrder.DataSource = dt;
gdOrder.DataBind();
if (dt.Rows.Count == 0)
{
btnOrder.Visible = false;
lblTotalAmount.Text = "";
}
else
{
btnOrder.Visible = true;
}
}
}
}
catch (Exception exMessage)
{
lblError.ForeColor = System.Drawing.Color.Red;
lblError.Text = exMessage.Message;
}
}
答案 0 :(得分:-1)
你必须用html表替换网格视图,并使用append函数填充表