Datalist更新文本框字段和其他字段丢失 [英] Datalist update textbox field and other field gone missing

查看:105
本文介绍了Datalist更新文本框字段和其他字段丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者

i我正在更新我的产品名称和价格

但如果我只更新我的价格,我的产品名称和图片就会丢失

只有我更新的字段会出现。



有人可以帮忙吗? ><< br mode =hold/>

这是我目前的代码



在AdminProduct.aspx.cs中



i'm a beginner here
i am updating my product name and price
but if i only update my price, my product name and images went missing
only the field that i update will appear.

could anyone help mi out? ><<br mode="hold" />
this is my current code

in AdminProduct.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            listbind();
        }
    }

    private void listbind()
    {
        string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);
        SqlCommand cmd = new SqlCommand("SELECT ProductID, ProductName, Price, Description, Picture FROM Product", myConnect);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "Product");
        DataList1.DataSource = ds;
        DataList1.DataBind();
    }





    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        //call edit item template for selected index
        DataList1.EditItemIndex = e.Item.ItemIndex;
        listbind();
    }

    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);
        int ProductID = (int)DataList1.DataKeys[(int)e.Item.ItemIndex];

        string strCommandText = "DELETE from Product WHERE ProductID=" + ProductID;
        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

        myConnect.Open();
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        myConnect.Close();
        DataList1.EditItemIndex = -1;
        listbind();

        
        
    }

    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = -1;//Call Item Template
        listbind();
    }

    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {

        int ProductID = Convert.ToInt32(Request.QueryString["ProductID"]);
        string Picture = "~/Image/" + Request.QueryString["Picture"];
        string ProductName;
        decimal Price;

        ProductID = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
        ProductName = ((TextBox)(e.Item.FindControl("txtProductName"))).Text;
        Price = Convert.ToDecimal(((TextBox)(e.Item.FindControl("txtPrice"))).Text);
        
        string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;


        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "UPDATE Product " + " SET ProductName=@aProductName, " + " Price=@aPrice, " + " Picture=@aPicture" + " WHERE ProductID=@aProductID ";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

        cmd.Parameters.Add("@aProductID", SqlDbType.Int).Value = ProductID;
        cmd.Parameters.Add("@aProductName", SqlDbType.NVarChar, 50).Value = ProductName;
        cmd.Parameters.Add("@aPrice", SqlDbType.Decimal).Value = Price;
        cmd.Parameters.Add("@aPicture", SqlDbType.NVarChar, 100).Value = Picture;

        myConnect.Open();
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        myConnect.Close();
        DataList1.EditItemIndex = -1;
        listbind();
    }




AdminProduct.aspx中的






in AdminProduct.aspx

<asp:DataList ID="DataList1" runat="server" CellPadding="4" 
        RepeatColumns="4" Width="1042px" 
        BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" 
        GridLines="Both" Height="369px" 
        OnCancelCommand="DataList1_CancelCommand" 
        OnEditCommand="DataList1_EditCommand"
        OnUpdateCommand="DataList1_UpdateCommand" 
        OnDeleteCommand="DataList1_DeleteCommand"
        DataKeyField="ProductID" RepeatDirection="Horizontal"
        >

        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
        <ItemStyle BackColor="White" ForeColor="#330099" HorizontalAlign="Center"/>
        
        
        <ItemTemplate>


            <asp:HyperLink ID="hlnk" NavigateUrl='<%# string.Format("ViewProductDetail.aspx?ProductID={0}&Picture={1}", Eval("ProductID"), Eval("Picture")) %>' runat="server">
                    
             <asp:Image ID="Image1" runat="server" Height="127px" 
                    ImageUrl='<%# Bind("Picture", "~/Image/{0}") %>' Width="129px" />
                    
                    </asp:HyperLink>
            <br />
            <asp:Label ID="ProductNameLabel" runat="server" 
                Text='<%# Eval("ProductName") %>' />
            <br />
            <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price","{0:C}") %>' />
            <br />

            <asp:LinkButton ID="b1" Text="Edit" CommandName="edit" runat="server" />
            <asp:LinkButton ID="b2" Text="Delete" CommandName="delete" runat="server" />

            <br />
            <br />
            <br />

        </ItemTemplate>


        <EditItemTemplate>
        <asp:HyperLink ID="hlnk" NavigateUrl='<%# string.Format("ViewProductDetail.aspx?ProductID={0}&Picture={1}", Eval("ProductID"), Eval("Picture")) %>' runat="server">
             <asp:Image ID="Image1" runat="server" Height="127px" 
                    ImageUrl='<%# Bind("Picture", "~/Image/{0}") %>' Width="129px" />
                    
                    </asp:HyperLink>
            <br />
            <asp:Label ID="ProductNameLabel" runat="server" 
                Text='<%# Eval("ProductName") %>' />
             
            <asp:TextBox ID="txtProductName" runat="server"></asp:TextBox>
            <br />
            <asp:Label ID="PriceLabel" runat="server" 
                Text='<%# Eval("Price","{0:C}") %>' />
            
            <asp:TextBox ID="txtPrice" runat="server"></asp:TextBox>
            <br />
            <asp:LinkButton ID="b4" Text="Update" CommandName="update" runat="server" />
            <asp:LinkButton ID="b3" Text="Cancel" CommandName="cancel" runat="server" />
            <br />
            </EditItemTemplate>


        <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
    </asp:DataList>



    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="Data Source=.\SQLEXPRESS;Initial Catalog=x;User ID=x;Password=x" 
        ProviderName="System.Data.SqlClient" 
        
        SelectCommand="SELECT [ProductName], [Price], [Picture], [ProductID] FROM [Product]">
    </asp:SqlDataSource>

推荐答案

在代码中,您正在更新这样的图片...

In code, you are updating Picture Like this...
string Picture = "~/Image/" + Request.QueryString["Picture"];



因此,例如,图片将变为〜/ Image / somImage.jpg



但你绑定它就像...


So, for instance, the Picture will become ~/Image/somImage.jpg.

But you are binding it like...

<asp:Image ID="Image1" runat="server" Height="127px" ImageUrl='<%# Bind("Picture", "~/Image/{0}") %>' Width="129px" />



因此, ImageUrl 变得像 〜/ Image /〜/ Image / somImage.jpg



这就是为什么它没有加载任何图像。

如果你看看rend,你会得到澄清的URL在浏览器中使用HTML并找到图像。


So, ImageUrl becomes something like ~/Image/~/Image/somImage.jpg.

That's why it is not loading any image.
You will get clarified of the URL if you look at the rendered HTML in Browser and locate the image.


这篇关于Datalist更新文本框字段和其他字段丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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