Click here to Skip to main content
14,974,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I used gridview binding in aspx page and code behind page .so for i got the error of need to remove any on definition.i want to remove aspx page binding .how do i do it

My Code Of Aspx Page is
C#
html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GridView</title>

    <script language="javascript" src="Validation.js" type="text/javascript"></script>

</head>
<body>
    <form id="form1" runat="server">
    <div>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="empid"
        ShowFooter="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="GridView1_RowCommand"
        OnRowCancelingEdit="GridView1_RowCancelingEdit1" OnRowDeleting="GridView1_RowDeleting1"
        OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
        OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
        onprerender="GridView1_PreRender" AllowSorting="True"
        onsorting="GridView1_Sorting"
        DataSourceID="SqlDataSource1">
        <PagerSettings FirstPageText="Next" Mode="NextPrevious" NextPageText="Prv" />
        <RowStyle BackColor="#EFF3FB" />
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:HyperLinkField DataNavigateUrlFields="empid" DataNavigateUrlFormatString="empdetails.aspx?empid={0}"
                Text="Pass Single Value" />
            <asp:HyperLinkField DataNavigateUrlFields="empid,empname,empage,empemail,empgender"
                DataNavigateUrlFormatString="empdetails.aspx?empid={0}&empname={1}&empage={2}&empemail={3}&empgender={4}"
                Text="Pass Multiple Values" />
            <asp:TemplateField HeaderText="empid" SortExpression="empid">
                <EditItemTemplate>
                    <asp:Label ID="lblempid" runat="server" Text='<%# Bind("empid")%>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:HiddenField runat="server" ID="HiddenField1" Value='<%#Eval("empid")%>'></asp:HiddenField>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="lblempid" runat="server"></asp:Label>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="empname" SortExpression="empname">
                <EditItemTemplate>
                    <asp:TextBox ID="txtempname" runat="server" Text='<%# Bind("empname") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="labempname" runat="server" Text='<%# Bind("empname") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtempname1" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="empage" SortExpression="empage">
                <EditItemTemplate>
                    <asp:TextBox ID="txtempage" runat="server" Text='<%# Bind("empage") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblempage" runat="server" Text='<%# Bind("empage") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtempage1" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="empemail" SortExpression="empemail">
                <EditItemTemplate>
                    <asp:TextBox ID="txtempemail" runat="server" Text='<%# Bind("empemail") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblempemail" runat="server" Text='<%# Bind("empemail") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtempemail1" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="empgender" SortExpression="empgender">
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlempgender" runat="server" SelectedValue='<%# Eval("empgender") %>'>
                        <asp:ListItem Value="male" Text="male"></asp:ListItem>
                        <asp:ListItem Value="female" Text="female"></asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblempgender" runat="server" Text='<%# Bind("empgender") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="ddlempgender1" runat="server">
                        <asp:ListItem Value="male" Text="male"></asp:ListItem>
                        <asp:ListItem Value="female" Text="female"></asp:ListItem>
                    </asp:DropDownList>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField ShowHeader="False">
                <EditItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                        Text="Update"></asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                        Text="Cancel"></asp:LinkButton>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                        Text="Edit"></asp:LinkButton>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Button ID="btnInsert" runat="server" Text="Insert" CommandName="Insert" OnClientClick="return validate()"></asp:Button>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowDeleteButton="True" />
        </Columns>
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:empdbConnectionString %>"
            SelectCommand="SELECT * FROM [emp]"></asp:SqlDataSource>
    <p>
        <asp:Button ID="Button1" runat="server" Text="Button1" PostBackUrl="~/empdetails.aspx"
            OnClick="Button1_Click" />
        </p>
            </div>
    </form>
</body>
</html>


My Code behind Page is

C#
public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["empDBConnectionString"].ConnectionString);

    protected void Page_Load(object sender, System.EventArgs e)
    {
        //Button btnInsert = (Button)GridView1.FooterRow.FindControl("btnInsert");
        //btnInsert.Attributes.Add("onclick", "return validate()");



        if (!IsPostBack)
        {
            GridFill();
        }

    }

    protected void GridFill()
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from emp", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();

            }
            else
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                GridView1.DataSource = ds;
                GridView1.DataBind();
                int columncount = GridView1.Rows[0].Cells.Count;
                GridView1.Rows[0].Cells.Clear();
                GridView1.Rows[0].Cells.Add(new TableCell());
                GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
                GridView1.Rows[0].Cells[0].Text = "No Records Found";
            }
        }
        catch
        {

        }
    }




thanks
Posted
Updated 12-Mar-12 1:07am
v3
Comments
El_Codero 12-Mar-12 6:50am
   
Not possible to help you without code. What do you mean with binding in page AND code behind? Regards
Herman<T>.Instance 12-Mar-12 6:51am
   
please elaborate your question. If you want to remove the binding, set DataSource = null;
Better show your code so we can see what your problem is.

1 solution

see your aspx design code, and reomve DataSourceID from gridview code
C#
<asp:gridview id="GridView1" runat="server" allowsorting="True" xmlns:asp="#unknown">
                    AutoGenerateColumns="False" CellPadding="4" DataKeyNames="taskid" 
                    DataSourceID="SqlDataSource1" ForeColor="#333333" 
                    Width="960px" AutoGenerateSelectButton="True" Font-Bold="False" 
                    Font-Italic="False" >
</asp:gridview>

use
C#
<asp:gridview id="GridView1" runat="server" allowsorting="True" xmlns:asp="#unknown">
                    AutoGenerateColumns="False" CellPadding="4" 
                     ForeColor="#333333" 
                    Width="960px" AutoGenerateSelectButton="True" Font-Bold="False" 
                    Font-Italic="False" >
</asp:gridview>
   
Comments
gokul.net 12-Mar-12 6:55am
   
If i remove the datasourceid in asp page the gridview events and controls would be change.so wt i do
uspatel 12-Mar-12 7:21am
   
only remove datasouse id..
bound filed remains as it is. then try

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900