GridView模板列编辑
绑定模板列,<ItemTemplate><Label /></ItemTemplate> LinkUrl(连接字符串),若字符串过长,则截取(Substring(0,50))前50个字符,后面的用"..."代替,但数遍移动到该字符串上面,提示(Toollip)字符串全文。
编辑模板列,需要添加<EditItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLink" runat="server" Text='<%# Eval("LinkUrl") %>' Width="320px">
</asp:TextBox>
</EditItemTemplate>
<asp:TextBox ID="txtLink" runat="server" Text='<%# Eval("LinkUrl") %>' Width="320px">
</asp:TextBox>
</EditItemTemplate>
<asp:TemplateField> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("LinkUrl").ToString().Length > 50? Eval("LinkUrl").ToString().Substring(0,50)+"...":Eval("LinkUrl") %>' ToolTip='<%# Eval("LinkUrl") %>'> </asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtLink" runat="server" Text='<%# Eval("LinkUrl") %>' Width="320px"> </asp:TextBox> </EditItemTemplate> <HeaderTemplate> <asp:Literal ID="Literal2" runat="server" Text="Link Url"></asp:Literal> </HeaderTemplate> <ItemStyle HorizontalAlign="Left" Width="320px" /> </asp:TemplateField>
截取字符串: <Label ID="lblLink" runat="server' Text='<%# Eval("LinkUrl").ToString().Length > 50? Eval("LinkUrl").ToString().Substring(0,50)+"...":Eval("LinkUrl") %>' ToolTip='<%# Eval("LinkUrl") %>' >
编辑,后台代码:
string linkUrl = (((TextBox)grdType.Rows[e.RowIndex].Cells[2].FindControl("txtLink")).Text).ToString().Trim();
protected void grdType_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string linkID = grdType.DataKeys[e.RowIndex].Value.ToString();
string linkText = (((TextBox)grdType.Rows[e.RowIndex].Cells[1].Controls[0]).Text).ToString().Trim();
string linkUrl = (((TextBox)grdType.Rows[e.RowIndex].Cells[2].FindControl("txtLink")).Text).ToString().Trim();
LinkBLL platBLL = new LinkBLL();//MVC架构,编辑Link部分
int i = platBLL.UpdateLink(linkText, linkUrl, Int32.Parse(linkID));
if (i > 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Update Link sucessfully!')", true);//提示编辑成功
}
grdType.EditIndex = -1;//释放当前所编辑的行
BindLink();//重新绑定GridView
}
Be the change you want to see in the world.