ObjectDataSource與GridView結合使用 ,GridView上的某一列使用模板列,此列的EditItemTemplate填充一個TEXTBOX
<EditItemTemplate>
         <asp:TextBox ID="txtDescription" runat="server" Text=<%# Bind("Description")%>></asp:TextBox>
</EditItemTemplate>
現問題如下:
我要控制此TEXTBOX為空時,不能保存。
但是ObjectDataSource與GridView結合使用的話,就不好控制阿。
見如下:
 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {            if (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtDescription"))).Text.ToString().Trim() == "")
            {
                //this.ObjectDataSource1.UpdateMethod = "";
            }
        }
請教各位給點提示

解决方案 »

  1.   

    难道没有人明白我的意思吗?我的意思就是我有个字段不能为空,所以当TEXTBOX为空,想不执行ObjectDataSource的UpdateMethod
      

  2.   

    aspx代碼  :
        
    <asp:GridView ID="GridView1" DataKeyNames ="CategoryID" runat="server" AutoGenerateColumns="False" DataSourceID ="ObjectDataSource1" OnRowUpdating ="GridView1_RowUpdating" OnRowEditing ="GridView1_RowEditing">
                <Columns>
                    <asp:TemplateField HeaderText="種類名稱">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtCategoryName" runat="server" Text=<%# Bind("CategoryName")%>></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtCategoryName" runat="server" ></asp:TextBox>
                        </FooterTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblCategoryName" runat="server" Text=<%# Eval("CategoryName")%> Width="96px"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="名稱解釋">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtDescription" runat="server" Text=<%# Bind("Description")%>></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtDescription" runat="server"></asp:TextBox>
                        </FooterTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblDescription" runat="server" Text=<%# Eval("Description")%> Width="116px"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="編輯">
                        <EditItemTemplate>
                            <asp:Button ID="Button4" runat="server" Text="保   存" CommandName="Update"  OnClick ="btnUpdate_Click"/>
                            <asp:Button ID="Button5" runat="server" Text="取   消" CommandName="Cancel"/>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:Button ID="Button3" runat="server" Text="新  增" CommandName="New" />
                        </FooterTemplate>
                        <ItemTemplate>
                            <asp:Button ID="Button1" runat="server" Text="修  改"  CommandName="Edit"/>
                            <asp:Button ID="Button2" runat="server" Text="刪  除"  CommandName="Delete" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"  TypeName ="DataAccess.test.ManufactureDAO" SelectMethod ="GetListCategories" DeleteMethod ="DeleteCategories" UpdateMethod ="UpdateCategories" InsertMethod ="AddCategories">
                <InsertParameters>
                            <asp:Parameter Name ="CategoryName" Type ="string" />
                            <asp:Parameter Name="Description" Type="string" />
                 </InsertParameters>
                 
                <DeleteParameters ><asp:Parameter Name ="CategoryID" Type ="int32" /></DeleteParameters>
                
                <UpdateParameters >
                            <asp:Parameter Name ="CategoryName" Type ="string" />
                            <asp:Parameter Name="Description" Type="string" />
                            <asp:Parameter Name ="CategoryID" Type ="int32" />
                </UpdateParameters>
                
            </asp:ObjectDataSource>cs代碼  :        protected void btnUpdate_Click(object sender, EventArgs e)
            { 
                //if ()此處若獲取到修改行的TEXTBOX就可以控制
            }protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                if (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtDescription"))).Text.ToString().Trim() == "")
                {
                      //此處怎么阻止ObjectDataSource1更新數據庫?
                      this.ObjectDataSource1.UpdateMethod = "";
                }
            }
      

  3.   

    objectdatasource.updateing事件里处理private void obs_Updating(object source, ObjectDataSourceMethodEventArgs e) {  // The names of the parameters are the same as
      // the variable names for the method that is invoked to
      // perform the Update. The InputParameters collection is
      // an IDictionary collection of name/value pairs,
      // not a ParameterCollection.
      if(string.IsNullEmpty(e.InputParameters[输入参数名]))
      {
         e.Cancel = true;
      }
    }
      

  4.   

    protected void obs_Updating(object source, ObjectDataSourceMethodEventArgs e) 
            {               // The names of the parameters are the same as 
                  // the variable names for the method that is invoked to 
                  // perform the Update. The InputParameters collection is 
                  // an IDictionary collection of name/value pairs, 
                  // not a ParameterCollection. 
                if (string.IsNullOrEmpty(Convert.ToString(e.InputParameters["Description"])))
                  {
                     Response.Write("<script>alert('不能為空!');</script>");
                     e.Cancel = true; 
                  } 
           } 
    whycom
    太感謝你了,呵呵