开发者

data grid with edit option error when click on edit

I am getting the following "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control." error. How can I avoid this error?

This is my code for editing the data from grid view:

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">


<asp:Panel ID="Panel1" runat="server" ScrollBars="Auto" Width="950px">
<asp:Label ID="lblMessage" runat="Server" ForeColor="Red"></asp:Label>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 

    CellPadding="2"

  DataKeyNames="DOC_id" ForeColor="Black" OnRowCancelingEdit="GridView1_RowCancelingEdit"

 OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" 
        OnRowUpdating="GridView1_RowUpdating" 
        onrowcommand="GridView1_AddRow" BackColor="LightGoldenrodYellow" 
        BorderColor="Tan" BorderWidth="1px" Height="392px" ShowFooter="True">

   <FooterStyle BackColor="Tan" />
            <AlternatingRowStyle BackColor="pink" />
    <Columns>
    <asp:templatefield showheader="False">  
            <edititemtemplate>  
                <asp:linkbutton causesvalidation="True" commandname="Update"  
     id="LinkButton1" runat="server" text="Update"></asp:linkbutton>  
                 <asp:linkbutton causesvalidation="False" commandname="Cancel" 
    id="LinkButton2" runat="server" text="Cancel"></asp:linkbutton> 

            </edititemtemplate>  
            <footertemplate>  
                <asp:linkbutton commandname="AddNew" id="AddNew" runat="server">Add 
  New</asp:linkbutton>  
            </footertemplate>  
            <itemtemplate>  
                <asp:linkbutton causesvalidation="False" commandname="Edit" id="LinkButton1"
   runat="server" text="Edit"></asp:linkbutton>  
            </itemtemplate>  
        </asp:templatefield>  
        <asp:TemplateField HeaderText="Delete?">
                    <ItemTemplate>
                        <span onclick="return confirm('Are you sure to Delete the 
   record?')">
                        <asp:LinkButton ID="lnkB" runat="Server" Text="Delete" 
   CommandName="Delete"></asp:LinkButton>
                        </span>
                        </ItemTemplate>
                        </asp:TemplateField>
        <asp:TemplateField HeaderText="DOC_id">
            <EditItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("DOC_id") %>'>
  </asp:Label>
            </EditItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID="NewTextBox22" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="NewRequiredFieldValidator22" runat="server" 
                    ErrorMessage="*" ControlToValidate="NewTextBox22">
  </asp:RequiredFieldValidator>

            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("DOC_id") %>'>
 </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Dr_Type">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Dr_Type") %>'>
 </asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ErrorMessage="*" ControlToValidate="TextBox1">  
  </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID="NewTextBox1" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="NewRequiredFieldValidator1" runat="server" 
                    ErrorMessage="*" ControlToValidate="NewTextBox1">    
   </asp:RequiredFieldValidator>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Dr_Type") %>'>
   </asp:Label>

            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Dr_Title">
            <EditItemTemplate>
            <asp:DropDownList ID="TextBox2" runat="server" selectedvalue='<%#      
 Bind("Dr_Title") %>'>
                <asp:ListItem Selected="True"></asp:ListItem>
                <asp:ListItem>Mr.</asp:ListItem>
                <asp:ListItem>Mrs.</asp:ListItem>
                <asp:ListItem>Ms.</asp:ListItem>
                <asp:ListItem>Fr.</asp:ListItem>
                <asp:ListItem>Sr.</asp:ListItem>
                <asp:ListItem>Rev.</asp:ListItem>
                <asp:ListItem>Dr.</asp:ListItem>
            </asp:DropDownList>

                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ErrorMessage="*" ControlToValidate="TextBox2">
    </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <FooterTemplate>
            <asp:DropDownList ID="NewTextBox2" runat="server" >
                <asp:ListItem Selected="True"></asp:ListItem>
                <asp:ListItem>Mr.</asp:ListItem>
                <asp:ListItem>Mrs.</asp:ListItem>
                <asp:ListItem>Ms.</asp:ListItem>
                <asp:ListItem>Fr.</asp:ListItem>
                <asp:ListItem>Sr.</asp:ListItem>
                <asp:ListItem>Rev.</asp:ListItem>
                <asp:ListItem>Dr.</asp:ListItem>
            </asp:DropDownList>
            <asp:RequiredFieldValidator ID="NewRequiredFieldValidator2" runat="server" 
                    ErrorMessage="*" ControlToValidate="NewTextBox2">
       </asp:RequiredFieldValidator>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Dr_Title") %>'>
  </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Dr_FirstName">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Dr_FirstName") 
     %>'></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                    ErrorMessage="*" ControlToValidate="TextBox3">
     </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID="NewTextBox3" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="NewRequiredFieldValidator3" runat="server" 
                    ErrorMessage="*" ControlToValidate="NewTextBox3">
      </asp:RequiredFieldValidator>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Bind("Dr_FirstName") %>'>
  </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Dr_MiddleName">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Dr_MiddleName") 
    %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                    ErrorMessage="*" ControlToValidate="TextBox4">
   </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID="NewTextBox4" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="NewRequiredFieldValidator4" runat="server" 
                    ErrorMessage="*" ControlToValidate="NewTextBox4">
  </asp:RequiredFieldValidator>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label5" runat="server" Text='<%# Bind("Dr_MiddleName") %>'>
  </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Dr_LastName">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Dr_LastName") %>'>
     </asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 
                    ErrorMessage="*" ControlToValidate="TextBox5">
   </asp:RequiredFieldValidator>

            </EditItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID="NewTextBox5" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="NewRequiredFieldValidator5" runat="server" 
                    ErrorMessage="*" ControlToValidate="NewTextBox5">
  </asp:RequiredFieldValidator>

            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label6" runat="server" Text='<%# Bind("Dr_LastName") %>'>
  </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Dr_Suffix">
            <EditItemTemplate>
            <asp:DropDownList ID="TextBox6" runat="server" selectedvalue='<%# 
      Bind("Dr_Suffix") %>'>
                <asp:ListItem Selected="True"></asp:ListItem>
                <asp:ListItem>Sr</asp:ListItem>
                <asp:ListItem>Jr</asp:ListItem>
                <asp:ListItem>III</asp:ListItem>
                <asp:ListItem>IV</asp:ListItem>
                <asp:ListItem>DMD</asp:ListItem>
                <asp:ListItem>DO</asp:ListItem>
                <asp:ListItem>DDS</asp:ListItem>
                <asp:ListItem>MD</asp:ListItem>
                <asp:ListItem>PHD</asp:ListItem>
                <asp:ListItem>DVM</asp:ListItem>
                <asp:ListItem>Esq</asp:ListItem>
                <asp:ListItem>Other</asp:ListItem>
            </asp:DropDownList>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" 

                    ErrorMessage="*" ControlToValidate="TextBox6">
     </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <FooterTemplate>
            <asp:DropDownList ID="NewTextBox6" runat="server">
                <asp:ListItem Selected="True"></asp:ListItem>
                <asp:ListItem>Sr</asp:ListItem>
                <asp:ListItem>Jr</asp:ListItem>
                <asp:ListItem>III</asp:ListItem>
                <asp:ListItem>IV</asp:ListItem>
                <asp:ListItem>DMD</asp:ListItem>
                <asp:ListItem>DO</asp:ListItem>
                <asp:ListItem>DDS</asp:ListItem>
                <asp:ListItem>MD</asp:ListItem>
                <asp:ListItem>PHD</asp:ListItem>
                <asp:ListItem>DVM</asp:ListItem>
                <asp:ListItem>Esq</asp:ListItem>
                <asp:ListItem>Other</asp:ListItem>
            </asp:DropDownList>
             <asp:RequiredFieldValidator ID="NewRequiredFieldValidator6" runat="server" 
                    ErrorMessage="*" ControlToValidate="NewTextBox6">
       </asp:RequiredFieldValidator>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label7" runat="server" Text='<%# Bind("Dr_Suffix") %>'>
   </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Dr_Degree">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Dr_Degree") %>'>
    </asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" 
                    ErrorMessage="*" ControlToValidate="TextBox7">
       </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID="NewTextBox7" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="NewRequiredFieldValidator7" runat="server" 
                    ErrorMessage="*" ControlToValidate="NewTextBox7">
       </asp:RequiredFieldValidator>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label8" runat="server" Text='<%# Bind("Dr_Degree") %>'>
      </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Dr_PreferredUsername">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox8" runat="server" Text='<%# 
      Bind("Dr_PreferredUsername") %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" 

                    ErrorMessage="*" ControlToValidate="TextBox8">
    </asp:RequiredFieldValidator>

            </EditItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID="NewTextBox8" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="NewRequiredFieldValidator8" runat="server" 
                    ErrorMessage="*" ControlToValidate="NewTextBox8">
     </asp:RequiredFieldValidator>

            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label9" runat="server" Text='<%# Bind("Dr_PreferredUsername")
      %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Dr_ContactNumber">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("Dr_ContactNumber")
     %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" 
                    ErrorMessage="*" ControlToValidate="TextBox9">
       </asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator3"
     runat="server" 
            ControlToValidate="TextBox9" 
            ErrorMessage="Invalid Contact Number....!!" ForeColor="Red" 
            ValidationExpression="^[9,8,7][0-9]{9}$"></asp:RegularExpressionValidator>
            </EditItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID="NewTextBox9" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="NewRequiredFieldValidator9" runat="server" 
                    ErrorMessage="*" ControlToValidate="NewTextBox9">
        </asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator4" 
       runat="server" 
            ControlToValidate="NewTextBox9" 
            ErrorMessage="Invalid Contact Number....!!" ForeColor="Red" 
            ValidationExpression="^[9,8,7][0-9]{9}$"></asp:RegularExpressionValidator>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label10" runat="server" Text='<%# Bind("Dr_ContactNumber") 
       %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Dr_FaxNumber">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("Dr_FaxNumber")
          %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" 
                    ErrorMessage="*" ControlToValidate="TextBox10">
          </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID="NewTextBox10" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="NewRequiredFieldValidator10" runat="server" 
                    ErrorMessage="*" ControlToValidate="NewTextBox10">
        </asp:RequiredFieldValidator>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label11" runat="server" Text='<%# Bind("Dr_FaxNumber") %>'>
      </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Dr_EmailAddress">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox11" runat="server" Text='<%# Bind("Dr_EmailAddress")
             %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" 
                    ErrorMessage="*" ControlToValidate="TextBox11">
   </asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator5" 
       runat="server" 
                ControlToValidate="TextBox11" ErrorMessage="Invalid Email ID....!!" 
       ForeColor="Red" 
                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
          </asp:RegularExpressionValidator>
            </EditItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID="NewTextBox11" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="NewRequiredFieldValidator11" runat="server" 
                    ErrorMessage="*" ControlToValidate="NewTextBox11">
        </asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator6" 
      runat="server" 
                ControlToValidate="NewTextBox11" ErrorMessage="Invalid Email ID....!!"
        ForeColor="Red" 
                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
        </asp:RegularExpressionValidator>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label12" runat="server" Text='<%# Bind("Dr_EmailAddress") 
        %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Dr_SpecialtyArea">
            <EditItemTemplate>
               <asp:DropDownList ID="TextBox12" runat="server" selectedvalue='<%#  
       Bind("Dr_SpecialtyArea") %>'
                DataSourceID="SqlDataSource3" DataTextField="Speciality" 
                DataValueField="Speciality">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                ConnectionString="<%$ ConnectionStrings:DBCS %>" 
                SelectCommand="SELECT [Speciality] FROM [Doc_Speciality]">
            </asp:SqlDataSource>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" 
                    ErrorMessage="*" ControlToValidate="TextBox12">
        </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <FooterTemplate>
               <asp:DropDownList ID="NewTextBox12" runat="server" 
                DataSourceID="SqlDataSource3" DataTextField="Speciality" 
                DataValueField="Speciality">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                ConnectionString="<%$ ConnectionStrings:DBCS %>" 
                SelectCommand="SELECT [Speciality] FROM [Doc_Speciality]">
            </asp:SqlDataSource>
            <asp:RequiredFieldValidator ID="NewRequiredFieldValidator12" runat="server" 
                    ErrorMessage="*" ControlToValidate="NewTextBox12">
          </asp:RequiredFieldValidator>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label13" runat="server" Text='<%# Bind("Dr_SpecialtyArea")
       %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

 <SelectedRowS开发者_如何学Pythontyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />

<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />

 <HeaderStyle BackColor="Tan" Font-Bold="True" />

            <SortedAscendingCellStyle BackColor="#FAFAE7" />
            <SortedAscendingHeaderStyle BackColor="#DAC09E" />
            <SortedDescendingCellStyle BackColor="#E1DB9C" />
            <SortedDescendingHeaderStyle BackColor="#C2A47B" />
</asp:GridView>

</asp:Panel>
<asp:LinkButton ID="AddNew"  runat="server" Text="AddNew" 
    onclientclick="imgbtn_Click" onclick="AddNew_Click"></asp:LinkButton>
</asp:Content>`


Try this solution:

http://w3ka.blogspot.com/2007/09/dropdownlist-in-gridview.html

You have to create a DataBound event for the DropDownLists that use the SqlDataSource and set the SelectedValue there.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜