在DataGrid的属性中选择"列",再选择按扭列中的"编辑 更新 取消",在运行时的网页中点击编辑无任何效果,是不是还要写代码,如果有是在哪里写,怎么写.

解决方案 »

  1.   

    如果是用那个东东的话
    在HTML端和代码端都要写代码比较麻烦
    还不如用模板列
    在里面加上Button就好处理多了(个人感觉)
      

  2.   

    HTML的我已经知道了,能不能在后台写
      

  3.   

    这两个是必须同时都有的
    如果你用的是"编辑 更新 取消"的话
    如果你用的是模板列的话
    就只用可以在模板列里放个Button再设置它的CommendName
    然后就可以直接在后台写代码了
    这样就不用在HTML端写那些烦人的东东了
      

  4.   

    如果你用的是"编辑 更新 取消"的话
    前台和后台虽都是必须的
    但前台的代码也可以放在后台来完成
    那就要用到控件的Attributes属性了
      

  5.   

    <asp:datagrid id="dg_Exec" CssClass="DATAGRID_DEFAULT" Width="100%" AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False" Runat="server">
    <AlternatingItemStyle CssClass="Result_DG_AlternatingItemStyle"></AlternatingItemStyle>
    <ItemStyle CssClass="Result_DG_ItemStyle"></ItemStyle>
    <HeaderStyle Wrap="False" HorizontalAlign="Center" CssClass="Result_DG_HeaderStyle"></HeaderStyle>
    <Columns>
    <asp:TemplateColumn HeaderText="Edit">
    <HeaderStyle HorizontalAlign="Center" Width="8%"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center" Width="8%"></ItemStyle>
    <ItemTemplate>
    <asp:ImageButton id="btn_DgEdit" CausesValidation="false" runat="server" CommandName="Edit" ImageUrl="images/edititem.gif" />
    </ItemTemplate>
    <EditItemTemplate>
    <asp:ImageButton ID="btn_DgOK" CausesValidation="false" runat="server" CommandName="OK" ImageUrl="images/save.gif" />
    <asp:ImageButton id="btn_DgCancel" runat="server" CausesValidation="false" CommandName="Cancel" ImageUrl="images/Exit.gif" />
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Delete">
    <HeaderStyle HorizontalAlign="Center" Width="7%"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center" Width="7%"></ItemStyle>
    <ItemTemplate>
    <asp:ImageButton id="btn_DgDelete" runat="server" CausesValidation="false" CommandName="Delete" ImageUrl="images/delete.gif" />
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="CMPY_CODE" ItemStyle-HorizontalAlign="Center" ReadOnly="True" HeaderText="CMPY_CODE"></asp:BoundColumn>
    <asp:BoundColumn DataField="LOCN_CODE" ItemStyle-HorizontalAlign="Center" ReadOnly="True" HeaderText="LOCN_CODE"></asp:BoundColumn>
    <asp:BoundColumn DataField="EMP_NAME" SortExpression="EMP_NAME" ItemStyle-HorizontalAlign="Center"
    ReadOnly="True" HeaderText="EMP_NAME"></asp:BoundColumn>
    <asp:TemplateColumn SortExpression="STAT_CODE" HeaderText="Exception Type">
    <HeaderStyle HorizontalAlign="Center" Width="20%"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center" Width="20%"></ItemStyle>
    <ItemTemplate>
    <asp:Label Width="100%" Runat="server" ID="lbl_STAT_CODE" text='<%#DataBinder.Eval(Container, "DataItem.STAT_CODE")%>' />
    </ItemTemplate>
    <EditItemTemplate>
    <asp:DropDownList ID="dpl_DGStatCode" Width="100%" Runat="server">
    <asp:ListItem Value="I">Included</asp:ListItem>
    <asp:ListItem Value="E">Excluded</asp:ListItem>
    </asp:DropDownList>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="EMP_NUM" Visible="False" ReadOnly="True" HeaderText="EMP_NUM"></asp:BoundColumn>
    <asp:BoundColumn DataField="UPDT_DATE" Visible="False" ReadOnly="True" HeaderText="UPDT_DATE"></asp:BoundColumn>
    </Columns>
    <PagerStyle HorizontalAlign="left" Position="Top" Mode="NumericPages"></PagerStyle>
    </asp:datagrid><asp:datagrid id="dg_ExecShow" CssClass="DATAGRID_DEFAULT" Width="100%" AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False" Runat="server">
    <AlternatingItemStyle CssClass="Result_DG_AlternatingItemStyle"></AlternatingItemStyle>
    <ItemStyle CssClass="Result_DG_ItemStyle"></ItemStyle>
    <HeaderStyle Wrap="False" HorizontalAlign="Center" CssClass="Result_DG_HeaderStyle"></HeaderStyle>
    <Columns>
    <ASP:TEMPLATECOLUMN HEADERTEXT="Import" ItemStyle-HorizontalAlign="Center">
    <HeaderStyle Wrap="False" />
    <ITEMTEMPLATE>
    <ASP:CHECKBOX ID="dg_chkImport" RUNAT="server" />
    </ITEMTEMPLATE>
    </ASP:TEMPLATECOLUMN>
    <asp:BoundColumn DataField="CMPY_CODE" ItemStyle-HorizontalAlign="Center" ReadOnly="True" HeaderText="CMPY_CODE"></asp:BoundColumn>
    <asp:BoundColumn DataField="LOCN_CODE" ItemStyle-HorizontalAlign="Center" ReadOnly="True" HeaderText="LOCN_CODE"></asp:BoundColumn>
    <asp:BoundColumn DataField="EMP_NAME" SortExpression="EMP_NAME" ItemStyle-HorizontalAlign="Center"
    ReadOnly="True" HeaderText="EMP_NAME"></asp:BoundColumn>
    <asp:BoundColumn DataField="EMP_NUM" Visible="False" ReadOnly="True" HeaderText="EMP_NUM"></asp:BoundColumn>
    </Columns>
    <PagerStyle HorizontalAlign="left" Position="Top" Mode="NumericPages"></PagerStyle>
    </asp:datagrid>
      

  6.   

    #region datagrid event
    /// <summary>
    /// page index change
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    private void dg_Exec_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    ComLogOutput.WriteLogFile("dsp_OtForExec.aspx datagrid pageindex change event start");
    dg_Exec.EditItemIndex = -1;
    dg_Exec.SelectedIndex = -1;
    dg_Exec.CurrentPageIndex = e.NewPageIndex;
    BindData(dg_Exec);
    ComLogOutput.WriteLogFile("dsp_OtForExec.aspx datagrid pageindex change event end");
    }
    /// <summary>
    /// dg sort
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    private void dg_Exec_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
    ComLogOutput.WriteLogFile("dsp_OtForExec.aspx datagrid sort event start");
    this.dg_Exec.CurrentPageIndex = 0;
    this.dg_Exec.EditItemIndex = -1 ;
    this.SetDropDownListStatus(1);
    if(hid_SortField.Value == e.SortExpression)
    {
    hid_SortType.Value = (hid_SortType.Value == ""? " Desc":"");
    }
    else
    {
    hid_SortField.Value = e.SortExpression;
    hid_SortType.Value = "";
    }
    BindData(dg_Exec);
    ComLogOutput.WriteLogFile("dsp_OtForExec.aspx datagrid sort event end");
    }
    private void dg_Exec_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    ListItemType lit = e.Item.ItemType;
    switch(lit)
    {
    case ListItemType.Item:
    case ListItemType.AlternatingItem:
    ImageButton btn_DgDelete =  (ImageButton)e.Item.FindControl("btn_DgDelete");
    ComJScript.Confirm(btn_DgDelete,ComLocalDiff.GetMessageById("0715",UserInfo.LangCode));
    break;
    case ListItemType.EditItem:
    // binding dropdownList to dataGrid
    DataRowView drv      = (DataRowView)e.Item.DataItem ;
    DropDownList dpl_DGStatCode          = (DropDownList)e.Item.FindControl("dpl_DGStatCode");
     
    if(!Convert.IsDBNull(drv) & drv != null)
    {

    dpl_DGStatCode.SelectedIndex = dpl_DGStatCode.Items.IndexOf(dpl_DGStatCode.Items.FindByValue(drv["STAT_CODE"].ToString()));
    }
    break;
    }
    } private void dg_Exec_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string strCompany   ;
    string strLocation ;
    string strEmpnum ;
    string strStat ;
    string strUid = UserInfo.UID; switch(e.CommandName)
    {
       // delete data form database
    case "Delete": 
    ComLogOutput.WriteLogFile("dsp_OtForExec.aspx datagrid delete command start");
    strCompany  = e.Item.Cells[2].Text;
    strLocation = e.Item.Cells[3].Text;
    strEmpnum = e.Item.Cells[6].Text;
    strStat   = e.Item.Cells[5].Text;
    try
    {
    OtForExecDB.UpdateData("Delete",strCompany,strLocation,strEmpnum,strStat,strUid);
    dg_Exec.CurrentPageIndex = 0;
    dg_Exec.EditItemIndex = -1;
    this.Session["OtForExec"] =null;
    SetHidSearchCondition();
    ComJScript.AlertNormal(this,ComLocalDiff.GetMessageById("0714",UserInfo.LangCode));
    BindData(dg_Exec);
    }
    catch(Exception ex)
    {
    ComLogOutput.WriteLogFile(ex);
    this.lbl_Err.Text = ComLocalDiff.GetMessageById("0001",UserInfo.LangCode);
    return;
    }
    ComLogOutput.WriteLogFile("dsp_OtForExec.aspx datagrid delete command end");
    break;
      //set datagrid status is edit
    case "Edit":
    ComLogOutput.WriteLogFile("dsp_OtForExec.aspx datagrid edit command start");
    this.dg_Exec.EditItemIndex = e.Item.ItemIndex;
    SetDropDownListStatus(0);
    BindData(dg_Exec);
    ComLogOutput.WriteLogFile("dsp_OtForExec.aspx datagrid edit command end");
    break;
                 //update data 
    case "OK":
    ComLogOutput.WriteLogFile("dsp_OtForExec.aspx datagrid update command start");
    strStat = ((DropDownList)e.Item.FindControl("dpl_DGStatCode")).SelectedItem.Value;
    string strLanguage  = UserInfo.LangCode;
    try
    {
     strCompany = e.Item.Cells[2].Text;
     strLocation = e.Item.Cells[3].Text;
     strEmpnum     = e.Item.Cells[6].Text;
     DateTime strUpdt = DateTime.Parse(e.Item.Cells[7].Text);
     strStat = ((DropDownList)e.Item.FindControl("dpl_DGStatCode")).SelectedItem.Value;
    OtForExecDB.CheckEmpExist(strCompany,strLocation,strEmpnum,strStat,"Update",strLanguage,strUpdt);
    OtForExecDB.UpdateData("Update",strCompany,strLocation,strEmpnum,strStat,strUid);
    dg_Exec.CurrentPageIndex     = 0;
    dg_Exec.EditItemIndex = -1;
    this.Session["OtForExec"] =null;
    ComJScript.AlertNormal(this,ComLocalDiff.GetMessageById("0713",UserInfo.LangCode));
    SetDropDownListStatus(1);
    this.hid_Exception.Value="*";
    BindData(dg_Exec);
    }
    catch(ApplicationException appex)
    {
    ComLogOutput.WriteLogFile(appex);
    this.lbl_Err.Text = appex.Message;
    return;
    }
    catch(Exception ex)
    {
    ComLogOutput.WriteLogFile(ex);
    this.lbl_Err.Text = ComLocalDiff.GetMessageById("0001",UserInfo.LangCode);
    return;
    }
    ComLogOutput.WriteLogFile("dsp_OtForExec.aspx datagrid update command end");
    break;
    //cancel update  operate
    case "Cancel":
    ComLogOutput.WriteLogFile("dsp_OtForExec.aspx datagrid cancle command start");
    dg_Exec.Columns[1].Visible=true;
    this.dg_Exec.EditItemIndex = -1 ;
    SetDropDownListStatus(1);
    BindData(dg_Exec);
    ComLogOutput.WriteLogFile("dsp_OtForExec.aspx datagrid cancle command end");
    break;
    }
    }