根据函数返回的结果禁用asp,net gridview中的编辑链接按钮 [英] disabling edit link button in asp,net gridview based on the result returned by a function

查看:45
本文介绍了根据函数返回的结果禁用asp,net gridview中的编辑链接按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i有以下情况:



3数据库表:工作,职位和分支

position和branch是查找表,其主键是作业表中的外键



在查找表中......当用户想要删除记录时,名为isDeleted的字段设置为1而不是从表中删除记录



我还有一个gridview,其select语句sql如下:

hi all,
i have the following scenario:

3 database tables: job, position and branch
position and branch are lookup tables whose primary keys are foreign keys in jobs table

In the lookup tables...when the user wants to delete a record, a field called isDeleted is set to 1 instead of deleting the record from the table

I also have a gridview whose select statement sql is the following:

SELECT Jobs.JobID, Branches.BranchName, Positions.PositionName, Jobs.YearsOfExperience, Jobs.IsArabicSpeaking, Jobs.Qualifications, Jobs.Role, Jobs.ExpireyDate, Jobs.isPublished FROM Branches INNER JOIN Jobs ON Branches.BranchID = Jobs.Branch INNER JOIN Positions ON Jobs.Position = Positions.PositionID



我的网格视图:


My Gridview :

<asp:GridView ID="gvJobs" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" DataKeyNames="JobID" OnSelectedIndexChanged="gvJobs_SelectedIndexChanged">
        <AlternatingRowStyle BackColor="#CCCCCC" />
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton Text="Edit" ID="lbEdit" OnClick="lbEdit_Click" Enabled="<%# CanBeEdited(Convert.ToInt32(gvJobs.SelectedDataKey.Value)) %>" runat="server" />
                    <span onclick="return confirm('Are you sure you want to delete?')">
                    <asp:LinkButton Text="Delete" ID="lbDelete" OnClick="lbDelete_Click" runat="server" />
                        </span>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="JobID" HeaderText="JobID" SortExpression="JobID" InsertVisible="False" ReadOnly="True" />
            <asp:BoundField DataField="BranchName" HeaderText="BranchName" SortExpression="BranchName" />
            <asp:BoundField DataField="PositionName" HeaderText="PositionName" SortExpression="PositionName" />
            <asp:BoundField DataField="YearsOfExperience" HeaderText="YearsOfExperience" SortExpression="YearsOfExperience" />
            <asp:CheckBoxField DataField="IsArabicSpeaking" HeaderText="IsArabicSpeaking" SortExpression="IsArabicSpeaking" />
            <asp:BoundField DataField="Qualifications" HeaderText="Qualifications" SortExpression="Qualifications" />
            <asp:BoundField DataField="Role" HeaderText="Role" SortExpression="Role" />
            <asp:BoundField DataField="ExpireyDate" HeaderText="ExpireyDate" SortExpression="ExpireyDate" />
            <asp:CheckBoxField DataField="isPublished" HeaderText="isPublished" SortExpression="isPublished" />
        </Columns>
        <FooterStyle BackColor="#CCCCCC" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="Gray" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#383838" />
    </asp:GridView>



问题:

如何在删除了查找值的记录中禁用gridview中的编辑链接按钮?



我试图通过创建一个返回布尔值的方法来解决这个问题,并使用该值来相应地设置编辑链接按钮的启用属性,如上面的gridview标记所示。



方法代码:


Problem:
how do i disable the edit link button in the gridview on a record that has a lookup value deleted ?

I attempted to tackle the problem by creating a method that returns a boolean and use that value to set the enabled property of the edit link button accordingly as you can see in the gridview markup above.

Method code:

public bool CanBeEdited(int id)
{
   bool valid = true;
   using (var db = new safatesNewsEntities())
   {
       var tmpJob=(from j in db.Jobs
                       where j.JobID==id
                       select j).FirstOrDefault();
       int p = tmpJob.Position;
       int b = tmpJob.Branch;
       var chkBranck = (from branch in db.Branches
                        where branch.BranchID == b
                        select branch).FirstOrDefault();
       var chkPos = (from pos in db.Positions
                     where pos.PositionID == p
                     select pos).FirstOrDefault();
       if (chkBranck.isDeleted || chkPos.isDeleted)
       {
           valid = false;
       }
   }
   return valid;
}





方法采用整数作为作业ID。该方法检查它是否可以在查找表中找到与已删除的感兴趣的作业记录中的branchID和positionID外键对应的行。如果当前记录删除了任何查找记录,则该方法返回false。



尝试的结果是:



the method takes an integer which is the job id. The method checks if it can find rows in the lookup tables that correspond to branchID and positionID foreign keys from the job record of interest that have been deleted. if the current record have any of the lookup records deleted, the method returns false.

The result of the attempt is:

Object reference not set to an instance of an object error





如果您需要进一步说明,请告诉我<非常感谢:)



please let me know if you need further explanation
Many thanks :)

推荐答案

我认为你的tmpJob为null请在选择位置和分支之前检查tmpJob是否为null />
i think your tmpJob is null please check the tmpJob is null or not before selecting the position and Branch
var tmpJob=(from j in db.Jobs
                       where j.JobID==id
                       select j).FirstOrDefault();
if(tmpJob!=null){
       int p = tmpJob.Position;
       int b = tmpJob.Branch;
}



希望这有帮助


Hope this helps


你好所有

我已经成功解决了这个问题通过将一些逻辑放入链接按钮的数据绑定方法中以检索datakey值,然后调用我的CanbeEdited方法将检索到的值作为参数传递。

这里是链接按钮的数据绑定方法:



hello all
I have successfully solved the problem by putting some logic into the databinding method of the link button to retrieve the datakey value and then call my CanbeEdited method passing the retrieved value as a parameter.
here is the databinding method of the link button:

protected void lbEdit_DataBinding(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            GridViewRow gvr = (GridViewRow)lb.NamingContainer;
            int i = gvr.RowIndex;
            int tempID = Convert.ToInt32(gvJobs.DataKeys[i].Value);
            lb.Enabled = CanBeEdited(tempID);
        }


这篇关于根据函数返回的结果禁用asp,net gridview中的编辑链接按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆