我有一个带有GridEditCommandColumn的RadGrid。我想让每行的EditText属性根据行的内容而改变。例如,根据行的内容将EditText从“编辑”更改为“编辑产品”或“编辑包”。
我尝试使用数据绑定语法但遇到错误,说它不支持数据绑定。
我是否可以通过编程方式在每行更改EditText的值?
答案 0 :(得分:1)
的.aspx
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnItemDataBound="RadGrid1_ItemDataBound">
<MasterTableView>
<Columns>
<telerik:GridEditCommandColumn ButtonType="LinkButton" UniqueName="GridEditCommandColumn"></telerik:GridEditCommandColumn>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:Label ID="lbl" runat="server" Text='<%# Eval("A") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
</asp:ScriptManager>
的.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("A");
string[] array = { "Product", "Goods", "Category", "Foods" };
// Loop & Add
for (int i = 0; i < array.Length; i++)
dt.Rows.Add(array[i]);
// Bind
RadGrid1.DataSource = dt;
RadGrid1.DataBind();
dt.Dispose();
}
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
// CHeck
if (e.Item is GridDataItem)
{
// Variable & Find Control
string value = string.Empty;
GridDataItem item = e.Item as GridDataItem;
LinkButton lbtnEdit = item["GridEditCommandColumn"].Controls[0] as LinkButton;
Label lbl = item.FindControl("lbl") as Label;
// Check & Set Value
if (lbl != null) value = lbl.Text.Trim();
// Check
if (lbtnEdit != null)
lbtnEdit.Text = "Edit " + value;
}
}