试图加入插入行到页脚在GridView的ASP.netGridView、行到页脚、net、ASP

2023-09-07 00:04:05 作者:默背你的心碎

我想给用户创造从页脚行的新纪录,我的事件处理程序似乎并不奏效的能力......也许我会在这个完全错误的。

插入按钮,我在GridView启用了也不行......检出网站的 http://aisched.engr.oregonstate.edu/admin/courses.aspx

下面是我的code。在前面和后面:

 公共部分类admin_courses:System.Web.UI.Page
{
公共表的表;
ListDictionary listValues​​ =新ListDictionary();
文本框textBox1的=新文本框(); //名称
文本框TextBox2中=新文本框(); // CR
文本框textBox3 =新文本框(); // CourseNum
文本框textBox4 =新文本框(); //部



保护无效Page_Init()
{

}

保护无效GridView1_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
    如果(e.Row.RowType = DataControlRowType.Footer!)回报; //逃生,如果没有页脚

    textBox1.ID =名;
    textBox1.Width = 250;
    textBox2.ID =credit_hours;
    textBox2.Width = 25;
    textBox3.ID =部门;
    textBox3.Width = 30;
    textBox4.ID =类;
    textBox4.Width = 25;

    LinkBut​​ton的增加=新的LinkBut​​ton();
    add.ID =添加;
    add.Text =添加课程;
    add.CommandName =添加;
    add.Click + =新的EventHandler(add_Click);

    e.Row.Cells [1] .Controls.Add(textBox1的);
    e.Row.Cells [2] .Controls.Add(TextBox2中);
    e.Row.Cells [3] .Controls.Add(textBox3);
    e.Row.Cells [4] .Controls.Add(textBox4);
    e.Row.Cells [5] .Controls.Add(添加);

}


公共无效add_Click(对象发件人,EventArgs的)
{
    回复于(你点击添加课程!);

        如果(textBox1.Text = NULL和放大器;!&安培; textBox2.Text = NULL和放大器;!&安培; textBox3.Text = NULL和放大器;!&安培;!textBox4.Text = NULL){
            listValues​​.Add(名,textBox1.Text);
            listValues​​.Add(credit_hours,textBox2.Text);
            listValues​​.Add(部门,textBox4.Text); //对于Visual
            listValues​​.Add(类,textBox3.Text);
        }
        LinqDataSource1.Insert(listValues​​);
        的Response.Redirect(〜/管理/ courses.aspx);
}
 

}

 <%@页面语言=C#的MasterPageFile =〜/ admin.masterAutoEventWireup =真正的codeFILE =courses.aspx.cs继承= admin_courses标题=OSU Aisched |管理员 - 课程%>

<%@注册议会=AjaxControlToolkit中命名空间=AjaxControlToolkit中标签preFIX =ASP%>

< ASP:内容ID =内容1ContentPlaceHolderID =admin_nav_links=服务器>
< UL ID =主>
    <李>< A HREF =overview.aspx>概述< / A>< /李>
    <李>< A HREF =users.aspx>用户< / A>< /李>
    <李类=current_page_item>< A HREF =courses.aspx>课程< / A>< /李>
    <李>< A HREF =programs.aspx>程序< / A>< /李>
    <李>< A HREF =sections.aspx>第< / A>< /李>
    <李>< A HREF =import.aspx>进出口LT; / A>< /李>
    <李>< A HREF =logs.aspx>日志< / A>< /李>
< / UL>
< / ASP:内容>
< ASP:内容ID =内容2ContentPlaceHolderID =ContentPlaceHolder1=服务器>
<表格ID =Form1的=服务器>
< ASP:ScriptManager的ID =ScriptManager1=服务器>
< / ASP:ScriptManager的>
< ASP:使用LinqDataSource ID =LinqDataSource1=服务器
ContextTypeName =DataClassesDataContextEnableDelete =真
EnableInsert =真EnableUpdate =真正的表名=课程>
< / ASP:使用LinqDataSource>
&其中; H1>&其中a取代;课程与所述; / a取代;&所述; / H1>
< ASP:的UpdateProgress ID =UpdateProgress1=服务器>
< / ASP:的UpdateProgress>
< ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =FALSE
    DataKeyNames =COURSE_ID的DataSourceID =LinqDataSource1
    背景色=#DEBA84边框颜色=#DEBA84边框=无边框宽度=1像素
    CELLPADDING =3CELLSPACING =2AllowSorting =真ShowFooter =真
    OnRowDataBound =GridView1_RowDataBound>
< RowStyle背景色=#FFF7E7前景色=#8C4510/>
<列>
    < ASP:绑定列的DataField =COURSE_IDHEADERTEXT =ID
        只读=真SORTEX pression =COURSE_ID/>
    < ASP:绑定列的DataField =名称HEADERTEXT =名称SORTEX pression =名/>
    < ASP:绑定列的DataField =credit_hoursHEADERTEXT =CR
        SORTEX pression =credit_hours/>
    < ASP:绑定列的DataField =部门HEADERTEXT =部SORTEX pression =部门/>
    < ASP:绑定列的DataField =类HEADERTEXT =#SORTEX pression =下课/>
    < ASP:CommandField中DeleteImageUrl =〜/媒体/ delete.png的ShowDeleteButton =真
        ShowEditButton =真ShowInsertButton =真/>
< /列>
< FooterStyle背景色=#F7DFB5前景色=#8C4510/>
< PagerStyle前景色=#8C4510Horizo​​ntalAlign =中心/>
< SelectedRowStyle背景色=#738A9C字体,粗体为真前景色=白/>
< HeaderStyle背景色=#A55129字体,粗体为真前景色=白/>
< / ASP:GridView控件>


< BR />
< ASP:超链接ID =HyperLink1=服务器目标=〜/管理/ addCourse.aspx启用=真正的NavigateUrl =〜/管理/ addCourse.aspx文本=添加新课程&GT ;< / ASP:超链接>
< BR />

< /形式GT;

< / ASP:内容>
 
在ASP.NET 2.0中操作数据之五十一 从GridView的页脚插入新记录

解决方案

我希望(或至少我当然希望)有一个更好的方式来做到这一点,但尝试这样的:

 < ASP:使用LinqDataSource =服务器ID =LinqDataSource1ContextTypeName =Courses.DataClassesDataContext表名=课程EnableDelete =真EnableUpdate =真EnableInsert =真/>
< H1>
    &其中a取代;
        课程< / A>< / H1>
< ASP:的UpdateProgress ID =UpdateProgress1=服务器>
< / ASP:的UpdateProgress>
< ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =FALSEDataKeyNames =COURSE_ID
    的DataSourceID =LinqDataSource1背景色=#DEBA84边框颜色=#DEBA84边框=无
    边框宽度=1像素的cellpadding =3CELLSPACING =2AllowSorting =真
    ShowFooter =真>
    <列>
        < ASP:绑定列的DataField =COURSE_IDHEADERTEXT =COURSE_ID只读=真SORTEX pression =COURSE_ID
            InsertVisible =FALSE/>
        < ASP:模板列HEADERTEXT =名称SORTEX pression =名与GT;
            < EditItemTemplate中>
                < ASP:文本框ID =TextBox1的=服务器文本='<%#绑定(名)%>'>< / ASP:文本框>
            < / EditItemTemplate中>
            <的ItemTemplate>
                < ASP:标签ID =标签1=服务器文本='<%#绑定(名)%>'>< / ASP:标签>
            < / ItemTemplate中>
            < FooterTemplate>
                < ASP:文本框ID =NameTextBox=服务器/>
            < / FooterTemplate>

        < / ASP:的TemplateField>
        < ASP:模板列HEADERTEXT =credit_hoursSORTEX pression =credit_hours>
            < EditItemTemplate中>
                < ASP:文本框ID =TextBox2中=服务器文本='<%#绑定(credit_hours)%>'>< / ASP:文本框>
            < / EditItemTemplate中>
            <的ItemTemplate>
                < ASP:标签ID =Label2的=服务器文本='<%#绑定(credit_hours)%>'>< / ASP:标签>
            < / ItemTemplate中>
            < FooterTemplate>
                < ASP:文本框ID =HoursTextBox=服务器/>
            < / FooterTemplate>
        < / ASP:的TemplateField>
        < ASP:模板列HEADERTEXT =部门SORTEX pression =部门>
            < EditItemTemplate中>
                < ASP:文本框ID =TextBox3=服务器文本='<%#绑定(部门)%>'>< / ASP:文本框>
            < / EditItemTemplate中>
            <的ItemTemplate>
                < ASP:标签ID =LABEL3=服务器文本='<%#绑定(部门)%>'>< / ASP:标签>
            < / ItemTemplate中>
            < FooterTemplate>
                < ASP:文本框ID =DeptTextBox=服务器/>
            < / FooterTemplate>
        < / ASP:的TemplateField>
        < ASP:模板列HEADERTEXT =下课SORTEX pression =类和GT;
            < EditItemTemplate中>
                < ASP:文本框ID =TextBox4=服务器文本='<%#绑定(类)%>'>< / ASP:文本框>
            < / EditItemTemplate中>
            <的ItemTemplate>
                < ASP:标签ID =Label4=服务器文本='<%#绑定(类)%>'>< / ASP:标签>
            < / ItemTemplate中>
            < FooterTemplate>
                < ASP:文本框ID =ClassTextBox=服务器/>
            < / FooterTemplate>
        < / ASP:的TemplateField>
        < ASP:的TemplateField ShowHeader =FALSE>
            < EditItemTemplate中>
                < ASP:LinkBut​​ton的ID =LinkBut​​ton1=服务器的CausesValidation =真的CommandName =更新
                    文本=更新>< / ASP:LinkBut​​ton的>
                &功放; NBSP;< ASP:LinkBut​​ton的ID =LinkBut​​ton2=服务器的CausesValidation =假的CommandName =取消
                    文本=取消>< / ASP:LinkBut​​ton的>
            < / EditItemTemplate中>
            <的ItemTemplate>
                < ASP:LinkBut​​ton的ID =LinkBut​​ton1=服务器的CausesValidation =假的CommandName =编辑
                    文本=编辑>< / ASP:LinkBut​​ton的>
                &功放; NBSP;< ASP:LinkBut​​ton的ID =LinkBut​​ton2=服务器的CausesValidation =假的CommandName =删除
                    文本=删除>< / ASP:LinkBut​​ton的>
            < / ItemTemplate中>
            < FooterTemplate>
                < ASP:LinkBut​​ton的ID =AddLinkBut​​ton=服务器的CommandName =添加文本=添加的CausesValidation =真/>
            < / FooterTemplate>
        < / ASP:的TemplateField>
    < /列>
< / ASP:GridView控件>

    保护无效的Page_Load(对象发件人,EventArgs的)
    {
        尝试
        {
            如果(的IsPostBack)
            {
                //我们在回发所以检查,看看是否AddLinkBut​​ton被点击
                字符串的eventTarget =的Request.Form [__ EVENTTARGET]的ToString()。

                如果(eventTarget.EndsWith(addlinkbutton,StringComparison.InvariantCultureIgnoreCase))
                {
                    //我们正在增加新的一行所以从尾行建立与控件ListDictionary
                    ListDictionary值=新ListDictionary();

                    values​​.Add(名,((文本框)GridView1.FooterRow.FindControl(NameTextBox))文本。);
                    values​​.Add(credit_hours,((文本框)GridView1.FooterRow.FindControl(HoursTextBox))文本。);
                    values​​.Add(部门,((文本框)GridView1.FooterRow.FindControl(DeptTextBox))文本。);
                    values​​.Add(类,((文本框)GridView1.FooterRow.FindControl(ClassTextBox))文本。);

                    //通过ListDictionary到数据源为关闭发送到数据库
                    LinqDataSource1.Insert(值);

                    //刷新网格,以便它显示了我们刚才添加的行
                    GridView1.DataBind();
                }
            }
        }
        赶上(例外)
        {

            扔;
        }
    }
 

我不能让它无需编写code手工做的插入工作。通过检查事件目标隐藏字段,看它是否与addlinkbutton'结尾感觉非常错误的,但它的工作原理处理AddLinkBut​​ton_Click事件在Page_Load事件。

I'm trying to give the user the ability to create a new record from the footer row and my event handler doesn't seem to be working... or maybe I'm going at this all wrong.

The insert button that I enabled in the gridview doesn't work either...checkout the site at http://aisched.engr.oregonstate.edu/admin/courses.aspx

Here is my code in front and behind:

public partial class admin_courses : System.Web.UI.Page
{
public Table table;
ListDictionary listValues = new ListDictionary();
TextBox textBox1 = new TextBox(); //Name
TextBox textBox2 = new TextBox(); //CR
TextBox textBox3 = new TextBox(); //CourseNum
TextBox textBox4 = new TextBox(); //Dept



protected void Page_Init()
{

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.Footer) return; //Escape if not footer

    textBox1.ID = "name";
    textBox1.Width = 250;
    textBox2.ID = "credit_hours";
    textBox2.Width = 25;
    textBox3.ID = "dept";
    textBox3.Width = 30;
    textBox4.ID = "class";
    textBox4.Width = 25;

    LinkButton add = new LinkButton();
    add.ID = "add";
    add.Text = "Add course";
    add.CommandName = "add";
    add.Click += new EventHandler(add_Click);

    e.Row.Cells[1].Controls.Add(textBox1);
    e.Row.Cells[2].Controls.Add(textBox2);
    e.Row.Cells[3].Controls.Add(textBox3);
    e.Row.Cells[4].Controls.Add(textBox4);
    e.Row.Cells[5].Controls.Add(add);

}


public void add_Click(object sender, EventArgs e)
{
    Response.Write("you Clicked Add Course!");

        if (textBox1.Text != null && textBox2.Text != null && textBox3.Text != null && textBox4.Text != null) {
            listValues.Add("name", textBox1.Text);
            listValues.Add("credit_hours", textBox2.Text);
            listValues.Add("dept", textBox4.Text); //For Visual
            listValues.Add("class", textBox3.Text);
        }
        LinqDataSource1.Insert(listValues);
        Response.Redirect("~/admin/courses.aspx");
}

}

<%@ Page Language="C#" MasterPageFile="~/admin.master" AutoEventWireup="true" CodeFile="courses.aspx.cs" Inherits="admin_courses" Title="OSU Aisched | Admin - Courses" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<asp:Content ID="Content1" ContentPlaceHolderID="admin_nav_links" Runat="Server">
<ul id="main">
    <li><a href="overview.aspx">Overview</a></li>
    <li><a href="users.aspx">Users</a></li>
    <li class="current_page_item"><a href="courses.aspx">Courses</a></li>
    <li><a href="programs.aspx">Programs</a></li>
    <li><a href="sections.aspx">Sections</a></li>
    <li><a href="import.aspx">Import</a></li>
    <li><a href="logs.aspx">Logs</a></li>
</ul>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<form id="Form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
ContextTypeName="DataClassesDataContext" EnableDelete="True" 
EnableInsert="True" EnableUpdate="True" TableName="courses">
</asp:LinqDataSource>
<h1><a>Courses</a></h1>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
</asp:UpdateProgress>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="course_id" DataSourceID="LinqDataSource1" 
    BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" 
    CellPadding="3" CellSpacing="2" AllowSorting="True" ShowFooter="True"
    OnRowDataBound="GridView1_RowDataBound" >
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<Columns>
    <asp:BoundField DataField="course_id" HeaderText="ID" 
        ReadOnly="True" SortExpression="course_id" />
    <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" />
    <asp:BoundField DataField="credit_hours" HeaderText="CR" 
        SortExpression="credit_hours" />
    <asp:BoundField DataField="dept" HeaderText="Dept" SortExpression="dept" />
    <asp:BoundField DataField="class" HeaderText="#" SortExpression="class" />
    <asp:CommandField DeleteImageUrl="~/media/delete.png" ShowDeleteButton="True" 
        ShowEditButton="True" ShowInsertButton="True"/>
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>


<br />
<asp:HyperLink ID="HyperLink1" runat="server" Target="~/admin/addCourse.aspx"  Enabled="true"NavigateUrl="~/admin/addCourse.aspx" Text="Add New course"></asp:HyperLink>
<br />

</form>

</asp:Content>

解决方案

I expect (or at least I certainly hope) there's a better way to do this, but try this:

<asp:LinqDataSource runat="server" ID="LinqDataSource1" ContextTypeName="Courses.DataClassesDataContext" TableName="Courses" EnableDelete="True" EnableUpdate="True" EnableInsert="True" />
<h1>
    <a>
        Courses</a></h1>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
</asp:UpdateProgress>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="course_id"
    DataSourceID="LinqDataSource1" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None"
    BorderWidth="1px" CellPadding="3" CellSpacing="2" AllowSorting="True" 
    ShowFooter="True">
    <Columns>
        <asp:BoundField DataField="course_id" HeaderText="course_id" ReadOnly="True" SortExpression="course_id"
            InsertVisible="False" />
        <asp:TemplateField HeaderText="name" SortExpression="name" >
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="NameTextBox" runat="server" />
            </FooterTemplate>

        </asp:TemplateField>
        <asp:TemplateField HeaderText="credit_hours" SortExpression="credit_hours">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("credit_hours") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("credit_hours") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="HoursTextBox" runat="server" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="dept" SortExpression="dept">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("dept") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("dept") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="DeptTextBox" runat="server" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="class" SortExpression="class">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("class") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Bind("class") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="ClassTextBox" runat="server" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <EditItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                    Text="Update"></asp:LinkButton>
                &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Cancel"></asp:LinkButton>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                    Text="Edit"></asp:LinkButton>
                &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
                    Text="Delete"></asp:LinkButton>
            </ItemTemplate>
            <FooterTemplate>
                <asp:LinkButton ID="AddLinkButton" runat="server" CommandName="Add" Text="Add" CausesValidation="true" />
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (IsPostBack)
            {
                // We are in a Postback so check to see if the AddLinkButton was clicked
                String eventTarget = Request.Form["__EVENTTARGET"].ToString();

                if (eventTarget.EndsWith("addlinkbutton",StringComparison.InvariantCultureIgnoreCase))
                {
                    // We are adding a new row so build a ListDictionary with the controls from the footer row
                    ListDictionary values = new ListDictionary();

                    values.Add("name", ((TextBox)GridView1.FooterRow.FindControl("NameTextBox")).Text);
                    values.Add("credit_hours", ((TextBox)GridView1.FooterRow.FindControl("HoursTextBox")).Text);
                    values.Add("dept", ((TextBox)GridView1.FooterRow.FindControl("DeptTextBox")).Text);
                    values.Add("class", ((TextBox)GridView1.FooterRow.FindControl("ClassTextBox")).Text);

                    // Pass the ListDictionary to the data source to send off to the database
                    LinqDataSource1.Insert(values);

                    // Refresh the grid so it shows the row we just added
                    GridView1.DataBind();
                }
            }
        }
        catch (Exception)
        {

            throw;
        }
    }

I couldn't make it work without writing code manually to do the Insert. Handling the AddLinkButton_Click event in the Page_Load event by examining the EventTarget hidden field to see if it ends with 'addlinkbutton' feels quite wrong, but it works.