I am afraid that it is not possible to directly use inline code to assign the value to a property of server controls.
There are only two approaches to do that:
Assign the Button1.CommandArgument = dt.Rows[i]["Manv"];
Use binding expression <%# "" %> within the data bind method, e.g, add the button inside the repeater and bind the value in the data bind event of the repeater.
// Simulation of the data
private static DataTable _dt;
public static DataTable dt
{
get
{
if (_dt is null)
{
_dt = new DataTable();
_dt.Columns.Add("Id", typeof(int));
_dt.Columns.Add("Manv", typeof(string));
_dt.Columns.Add("TenNV", typeof(string));
_dt.Columns.Add("START_TIME", typeof(string));
_dt.Columns.Add("FINISH_TIME", typeof(string));
_dt.Rows.Add(1, "Manv1", "TenNV1", new DateTime(2020,7,1),new DateTime(2020,7,2));
_dt.Rows.Add(2, "Manv2", "TenNV2", new DateTime(2020, 8, 1), new DateTime(2020, 8, 2));
_dt.Rows.Add(3, "Manv3", "TenNV3", new DateTime(2020, 9, 1), null);
_dt.Rows.Add(4, "Manv4", "TenNV4", null,null);
}
return _dt;
}
set
{
_dt = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRepeater();
}
}
private void BindRepeater()
{
repeater.DataSource = dt;
repeater.DataBind();
}
protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "start")
{
btnCmdArg.Text = e.CommandArgument.ToString() + " from button start in row " + e.Item.ItemIndex;
}
else if (e.CommandName == "finish")
{
btnCmdArg.Text = e.CommandArgument.ToString() + " from button finish in row " + e.Item.ItemIndex;
}
}
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView dr = (DataRowView)e.Item.DataItem;
if (dr["START_TIME"] != DBNull.Value)
{
((Button)e.Item.FindControl("btnStart")).Visible = false;
if(dr["FINISH_TIME"] != DBNull.Value)
{
((Button)e.Item.FindControl("Button1")).Visible = false;
}
}
}
}
Demo:
Hope this can help you.
Best regards,
Sean
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
None
0 Points
1 Post
Pass dynamic value CommandArgument
Aug 10, 2020 03:09 AM|mynoicity|LINK
Dear all,
I using boostrap and using public datatable in inline code.
Anyone help me pass value of datatable rows to CommandArgument button?
I am try like as below but can not get value.
CommandArgument='<%dt.Rows[i]["Manv"].ToString() %>'
Many thanks
Contributor
3020 Points
890 Posts
Re: Pass dynamic value CommandArgument
Aug 11, 2020 08:53 AM|Sean Fang|LINK
Hi mynoicity,
I am afraid that it is not possible to directly use inline code to assign the value to a property of server controls.
There are only two approaches to do that:
According your codes, I suggest you use repeater.
More details, you could refer to below codes:
ASPX:
Code behind:
Demo:
Hope this can help you.
Best regards,
Sean