如何填充下拉列表并向数据库添加值。 [英] How to populate dropdownlist and add values to database.

查看:70
本文介绍了如何填充下拉列表并向数据库添加值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类别页面,其中我将文本框数据添加为类别名称,图像作为categoryimage添加到数据库,并且还绑定到同一页面中的gridview。



现在我有另一个页面子类别

我想将类别页面中的数据,即categoryname拉入下拉列表,并在其下添加另一个文本框和图像。



在类别表ID中ImageName Imagepath。



任何人都可以帮助我在子类别页面中连接,以及sqldatabase设计如何链接这两个表。



我已经粘贴了类别和子类别表



类别表:

I have a category page wherein i am adding a textbox data as categoryname and an image as categoryimage to database and also binding to gridview in same page.

Now I have another page Subcategory
I want to pull the data from category page ie categoryname into dropdownlist and also add another textbox and image under it.

In category table ID ImageName Imagepath .

could anyone help me with connections in subcategory page as well as sqldatabase design as how to link the two tables.

I have pasted the category and subcategory table

CATEGORY TABLE:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Save Images In Folder and Display Images in Gridview from folder</title>
 <style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
}
</style>
<script type="text/javascript">
    function Confirmationbox() {
        var result = confirm('Are you sure you want to delete selected Category(s)?');
        if (result) {
            return true;
        }
        else {
            return false;
        }
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="Label1" runat="server" Text="Category Name" Width="100px" Height="16px"

            style="margin-top: 0px"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server" Width="180px" style="margin-top: 0px"

            Height="23px"></asp:TextBox>
    <br/>
    <br />
    <br />
    <asp:Label ID="Label3" runat="server" Text="Upload Image" Width="100px" Height="16px"

            style="margin-top: 0px"></asp:Label>
    <asp:FileUpload ID="fileuploadimages" runat="server" />
    <br />
    <br />
    <br />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
    </div>
    <div>
    <br />
    <br />
    <asp:GridView runat="server" DataKeyNames="ID" ID="gvImages" AutoGenerateColumns="False"

            DataSourceID="sqldataImages" CssClass="Gridview"

            HeaderStyle-BackColor="#61A6F8"

      onselectedindexchanged="gvImages_SelectedIndexChanged" >
    <Columns>
    <asp:BoundField DataField="ID" HeaderText="ID" />
    <asp:BoundField DataField="ImageName" HeaderText="CategoryName" />
    <asp:ImageField HeaderText="Image" DataImageUrlField="ImagePath" />
    <asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkdelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
     <%-- <asp:TemplateField HeaderText="Select">
     <ItemTemplate>
       <asp:LinkButton ID="LinkButton1"
         CommandArgument='<%# Eval("ID") %>'
         CommandName="Delete" runat="server">
         Delete</asp:LinkButton>
     </ItemTemplate>
   </asp:TemplateField>--%>
    </Columns>

<HeaderStyle BackColor="#61A6F8"></HeaderStyle>
    </asp:GridView>
    <asp:SqlDataSource ID="sqldataImages" runat="server"  ConnectionString="<%$ConnectionStrings:dbconnection %>"

    SelectCommand="select * from Category1"

    DeleteCommand="delete from Category1 where ID=@ID" >
    <DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
    </asp:SqlDataSource>
    </div>
    <asp:Button ID="btnDelete" runat="server" Text="Delete" onclick="btnDelete_Click" />
    </form>
</body>
</html>





.CS文件:



.CS file:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());

    protected void Page_Load(object sender, EventArgs e)
    {
        btnDelete.Attributes.Add("onclick", "javascript:return Confirmationbox()");
        //if (!IsPostBack)
        //{

        //}

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //Get Filename from fileupload control
        //string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
        string filename = TextBox1.Text;

        //Save images into Images folder
        fileuploadimages.SaveAs(Server.MapPath("Images/" + filename));
        //Getting dbconnection from web.config connectionstring

        //Open the database connection
        con.Open();
        //Query to insert images path and name into database
        SqlCommand cmd = new SqlCommand("Insert into Category1(ImageName,ImagePath) values(@ImageName,@ImagePath)", con);
        //Passing parameters to query
        cmd.Parameters.AddWithValue("@ImageName", filename);
        cmd.Parameters.AddWithValue("@ImagePath", "Images/" + filename);
        cmd.ExecuteNonQuery();
        //Close dbconnection
        con.Close();
        Response.Redirect("~/Default.aspx");
    }

    //protected void gvImages_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    int id = Convert.ToInt32(Gd_GalleryDetails.DataKeys[e.RowIndex].Value);
    //    ad.deletegalleryimage(id);
    //    msg.Show("Image deleted from gallery");
    //    fillgrid();


    //private void BindData(string Query)
    //{
    //{
    //    con.Open();
    //    using (SqlCommand comm = new SqlCommand(Query +
    //                  "; select * from Category1 ", con))
    //    {
    //        SqlDataAdapter da = new SqlDataAdapter(con);
    //        DataSet ds = new DataSet();
    //        da.Fill(ds);
    //        gvImages.DataSource = ds;
    //        gvImages.DataBind();
    //    }
    //  }
    //}

    //    protected void gvImages_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    string EmployeeID = gvImages.DataKeys[e.RowIndex].Value.ToString();
    //    string Query = "delete Category1 where Category1.Category1ID =" +Category1ID;
    //    BindData(Query);
    //}

    //protected void gvImages_RowDataBound(object sender,
    //                     GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {
    //        LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
    //        l.Attributes.Add("onclick", "javascript:return " +
    //        "confirm('Are you sure you want to delete this record " +
    //        DataBinder.Eval(e.Row.DataItem, "CategoryID") + "')");
    //    }
    //}

    //protected void gvImages_RowCommand(object sender,
    //                         GridViewCommandEventArgs e)
    //{
    //    if (e.CommandName == "Delete")
    //    {
    //        // get the categoryID of the clicked row
    //        int ID = Convert.ToInt32(e.CommandArgument);
    //        // Delete the record
    //        DeleteRecordByID(ID);
    //        // Implement this on your own :)
    //    }
    //}

    //protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    int ID = (int)gvImages.DataKeys[e.RowIndex].Value;
    //    DeleteRecordByID(ID);
    //}

    protected void gvImages_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {

        //Loop through all the rows in gridview
        foreach (GridViewRow gvrow in gvImages.Rows)
        {
            //Finiding checkbox control in gridview for particular row
            CheckBox chkdelete = (CheckBox)gvrow.FindControl("chkdelete");
            //Condition to check checkbox selected or not
            if (chkdelete.Checked)
            {
                //Getting UserId of particular row using datakey value
                int UID = Convert.ToInt32(gvImages.DataKeys[gvrow.RowIndex].Value);
                sqldataImages.DeleteParameters["ID"].DefaultValue = UID.ToString();
                sqldataImages.Delete();
            }
        }

    }
}





SUBCATEGORY PAGE:



SUBCATEGORY PAGE:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="subcategory.aspx.cs" Inherits="subcategory" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="CategoryName" Width ="150px"></asp:Label>
       &nbsp;<asp:DropDownList ID="DropDownList1" runat="server" Height="36px" Width="150px">
        </asp:DropDownList>
        <br />
<br />
    </div>
    <asp:Label ID="Label2" runat="server" Text="SubcategoryName"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <br />
    Image&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <br />
    <br />
    <asp:Label ID="Label3" runat="server" Text="Description"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TextBox2" runat="server" style="margin-left: 0px"

        Width="159px"></asp:TextBox>
    <br />
    <br />
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Button1" runat="server" Text="Add"  Width="100px"

        onclick="Button1_Click"/>
    </form>
</body>
</html>

推荐答案

ConnectionStrings:dbconnection %>\"

SelectCommand=\"select * from Category1\"

DeleteCommand=\"delete from Category1 where ID=@ID\" >
<DeleteParameters>
<asp:Parameter Name =\"ID\" Type=\"Int32\" />
</DeleteParameters>
</asp:SqlDataSource>
</div>
<asp:Button ID=\"btnDelet e\" runat=\"server\" Text=\"Delete\" onclick=\"btnDelete_Click\" />
</form>
</body>
</html>
ConnectionStrings:dbconnection %>" SelectCommand="select * from Category1" DeleteCommand="delete from Category1 where ID=@ID" > <DeleteParameters> <asp:Parameter Name="ID" Type="Int32" /> </DeleteParameters> </asp:SqlDataSource> </div> <asp:Button ID="btnDelete" runat="server" Text="Delete" onclick="btnDelete_Click" /> </form> </body> </html>




.CS file:



.CS file:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());

    protected void Page_Load(object sender, EventArgs e)
    {
        btnDelete.Attributes.Add("onclick", "javascript:return Confirmationbox()");
        //if (!IsPostBack)
        //{

        //}

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //Get Filename from fileupload control
        //string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
        string filename = TextBox1.Text;

        //Save images into Images folder
        fileuploadimages.SaveAs(Server.MapPath("Images/" + filename));
        //Getting dbconnection from web.config connectionstring

        //Open the database connection
        con.Open();
        //Query to insert images path and name into database
        SqlCommand cmd = new SqlCommand("Insert into Category1(ImageName,ImagePath) values(@ImageName,@ImagePath)", con);
        //Passing parameters to query
        cmd.Parameters.AddWithValue("@ImageName", filename);
        cmd.Parameters.AddWithValue("@ImagePath", "Images/" + filename);
        cmd.ExecuteNonQuery();
        //Close dbconnection
        con.Close();
        Response.Redirect("~/Default.aspx");
    }

    //protected void gvImages_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    int id = Convert.ToInt32(Gd_GalleryDetails.DataKeys[e.RowIndex].Value);
    //    ad.deletegalleryimage(id);
    //    msg.Show("Image deleted from gallery");
    //    fillgrid();


    //private void BindData(string Query)
    //{
    //{
    //    con.Open();
    //    using (SqlCommand comm = new SqlCommand(Query +
    //                  "; select * from Category1 ", con))
    //    {
    //        SqlDataAdapter da = new SqlDataAdapter(con);
    //        DataSet ds = new DataSet();
    //        da.Fill(ds);
    //        gvImages.DataSource = ds;
    //        gvImages.DataBind();
    //    }
    //  }
    //}

    //    protected void gvImages_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    string EmployeeID = gvImages.DataKeys[e.RowIndex].Value.ToString();
    //    string Query = "delete Category1 where Category1.Category1ID =" +Category1ID;
    //    BindData(Query);
    //}

    //protected void gvImages_RowDataBound(object sender,
    //                     GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {
    //        LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
    //        l.Attributes.Add("onclick", "javascript:return " +
    //        "confirm('Are you sure you want to delete this record " +
    //        DataBinder.Eval(e.Row.DataItem, "CategoryID") + "')");
    //    }
    //}

    //protected void gvImages_RowCommand(object sender,
    //                         GridViewCommandEventArgs e)
    //{
    //    if (e.CommandName == "Delete")
    //    {
    //        // get the categoryID of the clicked row
    //        int ID = Convert.ToInt32(e.CommandArgument);
    //        // Delete the record
    //        DeleteRecordByID(ID);
    //        // Implement this on your own :)
    //    }
    //}

    //protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    int ID = (int)gvImages.DataKeys[e.RowIndex].Value;
    //    DeleteRecordByID(ID);
    //}

    protected void gvImages_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {

        //Loop through all the rows in gridview
        foreach (GridViewRow gvrow in gvImages.Rows)
        {
            //Finiding checkbox control in gridview for particular row
            CheckBox chkdelete = (CheckBox)gvrow.FindControl("chkdelete");
            //Condition to check checkbox selected or not
            if (chkdelete.Checked)
            {
                //Getting UserId of particular row using datakey value
                int UID = Convert.ToInt32(gvImages.DataKeys[gvrow.RowIndex].Value);
                sqldataImages.DeleteParameters["ID"].DefaultValue = UID.ToString();
                sqldataImages.Delete();
            }
        }

    }
}





SUBCATEGORY PAGE:



SUBCATEGORY PAGE:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="subcategory.aspx.cs" Inherits="subcategory" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="CategoryName" Width ="150px"></asp:Label>
       &nbsp;<asp:DropDownList ID="DropDownList1" runat="server" Height="36px" Width="150px">
        </asp:DropDownList>
        <br />
<br />
    </div>
    <asp:Label ID="Label2" runat="server" Text="SubcategoryName"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <br />
    Image&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <br />
    <br />
    <asp:Label ID="Label3" runat="server" Text="Description"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TextBox2" runat="server" style="margin-left: 0px"

        Width="159px"></asp:TextBox>
    <br />
    <br />
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Button1" runat="server" Text="Add"  Width="100px"

        onclick="Button1_Click"/>
    </form>
</body>
</html>


add this column in subcategory table



ID CategoryID ImageName Imagepath



In subcategory page bind category in category’s dropdown now on submit insert category id insert into CategoryID column and other co lumn are same as you inserted into category page.
add this column in subcategory table

ID CategoryID ImageName Imagepath

In subcategory page bind category in category's dropdown now on submit insert category id insert into CategoryID column and other column are same as you inserted into category page.


这篇关于如何填充下拉列表并向数据库添加值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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