开发者

How to Deal with Null (GridView Container.DataItem)

开发者 https://www.devze.com 2023-02-05 15:41 出处:网络
whats wrong here, why is it complaining about closing the \")\" ? <asp:TemplateField HeaderText=\"Name\" SortExpression=\"Name\">

whats wrong here, why is it complaining about closing the ")" ?

<asp:TemplateField HeaderText="Name" SortExpression="Name">
                    <ItemTemplate>   
                        <%# FixNull((namespace.Student)(Container.DataItem), "Name"); %>               
                    </ItemTemplate>
                    <EditItemTemplate>
                       <asp:dropdownlist id="ddlStudent" runat="server" datasourceid="CountyDataSource"
                            datatextfield="Name" datavaluefield="I开发者_如何学Pythond" autopostback="false">
                        </asp:dropdownlist>
                    </EditItemTemplate>
                </asp:TemplateField>

 protected string FixNull(object dataItem, string fieldName)
 {
        if (dataItem != null)
        {
            System.Data.DataRowView cls = (System.Data.DataRowView)dataItem;
            if (!cls.Row.IsNull(fieldName))
                return (cls[fieldName]).ToString();
            else
                return "&nbsp;";
        }
        else
            return "&nbsp;";
    }



Compiler Error Message: CS1026: ) expected

Source Error: Line: 133 


Line 131:                  <asp:TemplateField HeaderText="Name" SortExpression="Name">
Line 132:                    <ItemTemplate>   
Line 133:    <%# FixNull((namespace.Student)(Container.DataItem), "Name"); %>               
Line 134:                    </ItemTemplate>
Line 135:                    <EditItemTemplate>


<%# Eval("CountyName") == null ? "&nbsp;" : Eval("CountyName")%>   


I think, you do not need the ;

it should just read:

<%# FixNull((namespace.Student)(Container.DataItem), "Name") %>


To simplify further you could use

<%# this.Eval("CountyName") ?? "&nbsp;"%>


Maybe try something like this:

 <asp:TemplateField HeaderText="Name" SortExpression="Name">
    <ItemTemplate>   
        <%# FixNull(DataBinder.Eval(Container.DataItem, "Name"))%>               
    </ItemTemplate>
    <EditItemTemplate>
        <asp:dropdownlist id="ddlStudent" runat="server" datasourceid="CountyDataSource"
            datatextfield="Name" datavaluefield="Id" autopostback="false">
        </asp:dropdownlist>
    </EditItemTemplate>
</asp:TemplateField>


protected string FixNull(string dataItem)
{
    if (!string.IsNullOrEmpty(dataItem))
    {
        return dataItem;
    }
    return "&nbsp;";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消