在asp中使用row命令更新gridview行

时间:2017-08-15 19:57:05

标签: c# asp.net gridview textbox

我想要更新行网格视图,但是,不从文本框网格视图中获取值

// c#code

if(e.CommandName=="UpdateRow")
{
    int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
    int pid = Convert.ToInt32(e.CommandArgument);
    string name =((TextBox)GridPatient.Rows[rowIndex].FindControl("txtname")).Text.ToString();
    string lastname= ((TextBox)GridPatient.Rows[rowIndex].FindControl("txtlastname")).Text;
    string phone= ((TextBox)GridPatient.Rows[rowIndex].FindControl("txtphone")).Text;
    string nationalcode= ((TextBox)GridPatient.Rows[rowIndex].FindControl("txtnationalcode")).Text;
    string age=((TextBox)GridPatient.Rows[rowIndex].FindControl("txtage")).Text;
    string sex= ((TextBox)GridPatient.Rows[rowIndex].FindControl("txtsex")).Text;
    string address= ((TextBox)GridPatient.Rows[rowIndex].FindControl("txtaddress")).Text;
    BLPatient.UpdatePatient(name, lastname, phone, nationalcode, Convert.ToInt32(age), sex, address, pid);
    GridPatient.EditIndex = -1;
    GridPatient.DataSource = BLPatient.GetPatient();
    GridPatient.DataBind();
}

// html代码

<Columns>

    <asp:TemplateField HeaderText="کد بیمار">
        <ItemTemplate>
            <asp:Label Text='<%# Eval("PatientID") %>' runat="server" />
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="نام ">
        <ItemTemplate>
            <asp:Label Text='<%# Eval("PatientName") %>' runat="server" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtname" Text='<%# Eval("PatientName") %>' CssClass="form-control" />
        </EditItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText=" نام خانوادگی">
        <ItemTemplate>
            <asp:Label Text='<%# Eval("PatientLastName") %>' runat="server" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtlastname" Text='<%# Eval("PatientLastName") %>' CssClass="form-control" />
        </EditItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText=" تلفن">
        <ItemTemplate>
            <asp:Label Text='<%# Eval("PatientPhone") %>' runat="server" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtphone" Text='<%# Eval("PatientPhone") %>' CssClass="form-control" />
        </EditItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="شماره ملی">
        <ItemTemplate>
            <asp:Label Text='<%# Eval("PatientNationalCode") %>' runat="server" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtnationalcode" Text='<%# Eval("PatientNationalCode") %>' CssClass="form-control" />
        </EditItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="سن">
        <ItemTemplate>
            <asp:Label Text='<%# Eval("PatientAge") %>' runat="server" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtage" Text='<%# Eval("PatientAge") %>' CssClass="form-control" />
        </EditItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="جنسیت">
        <ItemTemplate>
            <asp:Label Text='<%# Eval("PatientSex") %>' runat="server" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtsex" Text='<%# Eval("PatientSex") %>' CssClass="form-control" />
        </EditItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="آدرس">
        <ItemTemplate>
            <asp:Label Text='<%# Eval("PatientAddress") %>' runat="server" />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox runat="server" ID="txtaddress" Text='<%# Eval("PatientAddress") %>' CssClass="form-control" />
        </EditItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="عملیات">
        <ItemTemplate>
            <asp:LinkButton CommandName="DeleteRow" CommandArgument='<%# Eval("PatientID") %>' runat="server" ToolTip="حذف اطلاعات"><i class="fa fa-trash" style="color:orangered"></i></asp:LinkButton>
            <asp:LinkButton CommandName="EditRow" CommandArgument='<%# Eval("PatientID") %>' runat="server" ToolTip="ویرایش اطلاعات"><i class="fa fa-edit" style="color:cornflowerblue"></i></asp:LinkButton>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:LinkButton CommandArgument='<%# Eval("PatientID") %>' CommandName="UpdateRow" runat="server" ToolTip="ویرایش اطلاعات"><i class="fa fa-check-circle" style="color:forestgreen"></i></asp:LinkButton>
            <asp:LinkButton CommandArgument='<%# Eval("PatientID") %>' CommandName="CancelUpdate" runat="server" ToolTip="انصراف"><i class="fa fa-ban" style="color:red"></i></asp:LinkButton>
        </EditItemTemplate>
    </asp:TemplateField>

</Columns>
enter code here

0 个答案:

没有答案