Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a Datagrid control which displays Id, Image. When I am clicking on edit button a button is there and after click that i want to open a modal popup extender which will be assigned to a panel that contains a fileupload control, ok and cancel button. The problem is when I press the ok button the button_click event I am not able to find the file inside the FileUpload control.

Here is my aspx page code:
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
    <triggers>
        <asp:AsyncPostBackTrigger ControlID="dgnews" />
    </triggers>
    <contenttemplate>
    <asp:DataGrid ID="dgnews" runat="server" AutoGenerateColumns="false" 
                    oncancelcommand="dgnews_cancel" ondeletecommand="dgnews_delete" 
                    oneditcommand="dgnews_edit" onpageindexchanged="dgnews_PageIndexChanged" 
                    onupdatecommand="dgnews_update" Width="715px" AllowPaging="true" 
            AllowSorting="true" DataKeyField="res_id"  PageSize="10" 
            onitemdatabound="dgnews_ItemDataBound" >
                    <pagerstyle horizontalalign="Center" mode="NumericPages">
                         Position="TopAndBottom"/>
                    <columns>
                    <asp:BoundColumn DataField="res_id" Visible="false" HeaderText="">
                            <asp:TemplateColumn HeaderText="Image">
                                <itemtemplate>
                                    <ul style="padding-removed15px;">
                                       <a href="#"><img id="imgSmall" src='<%# DataBinder.Eval(Container, "DataItem.res_image") %>' alt="Gallery Image" width="50" height="50" /></a>
                                    </ul>                                            
                                </itemtemplate>
                                <edititemtemplate>
                                    <asp:Button ID="aaa" Text="Select Images" runat="server"/>
                                        <cc1:ModalPopupExtender BackgroundCssClass="modalBackground"
    CancelControlID="Button2"  runat="server" PopupControlID="picuploader" ID="ModalPopupExtender1"
    TargetControlID="aaa" />
    <asp:Panel ID="picuploader" runat="server" CssClass="modalPopup" DefaultButton="Button1" Style="display:none;">
    <asp:UpdatePanel ID="UpdatePanel2" UpdateMode="Conditional" runat="server">
    <triggers>
    <asp:AsyncPostBackTrigger ControlID="Button1" />
    </triggers>
    <contenttemplate>
            <table>
            <tr>
                <td>
                    <asp:Image Width="50px" Height="50px" ID="Image1" runat="server" /></td>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" /></td>
            </tr>
            <tr>
                <td>
                    <asp:Image Width="50px" Height="50px" ID="Image2" runat="server" /></td>
                <td>
                <asp:FileUpload ID="FileUpload2"   runat="server" /></td>
            </tr>
            <tr>
                <td>
                    <asp:Image ID="Image3" runat="server" Width="50px" Height="50px" /></td>
                <td>
                <asp:FileUpload ID="FileUpload3"   runat="server" /></td>
            </tr>
            <tr>
                <td>
                    <asp:Image ID="Image4" runat="server" Width="50px" Height="50px"/></td>
                <td>
                <asp:FileUpload ID="FileUpload4"  runat="server" /></td>
            </tr>
            <tr>
                <td></td>
                <td><asp:Button ID="Button1" runat="server" Text="ok" OnClick="UploadPic" />
                    <asp:Button ID="Button2" runat="server" Text="cancel" /></td>
            </tr>
            </table>
            </contenttemplate>
</edititemtemplate>         
                <asp:EditCommandColumn CancelText="Cancel" EditText="Edit" HeaderText="Modify" UpdateText="Update"/>
                            <asp:ButtonColumn CommandName="Delete" Text="Delete" HeaderText="Remove"/>                                           
                </columns>
                    
    </pagerstyle></contenttemplate>


and cs is as follows:
ClsCommon cls = new ClsCommon();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        BindGrid();
    }
}
protected void BindGrid()
{

}
protected void dgnews_cancel(object source, DataGridCommandEventArgs e)
{
    dgnews.EditItemIndex = -1;
    BindGrid();
}

protected void dgnews_delete(object source, DataGridCommandEventArgs e)
{
    try
    {

    }
    catch (Exception ex)
    {

    }
}

protected void dgnews_edit(object source, DataGridCommandEventArgs e)
{
    dgnews.EditItemIndex = e.Item.ItemIndex;
    BindGrid();
    Session["ID"] = dgnews.DataKeys[e.Item.ItemIndex];
}

protected void dgnews_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
    dgnews.CurrentPageIndex = e.NewPageIndex;
    BindGrid();
}

protected void dgnews_update(object source, DataGridCommandEventArgs e)
{

}

protected void UploadPic(object sender, EventArgs e)
{
    if (FileUpload1.PostedFile != null)
    {
        string[] words = FileUpload1.FileName.Split('.');
        string d_name = words[1];
        HttpContext.Current.Session["SessionKey"] = System.Guid.NewGuid().ToString() + "." + d_name;
        cls.UpdateData("_res", new string[] { "res_image" }, new string[] { Session["SessionKey"].ToString() }, "where res_id='" + Session["ID"].ToString() + "'", "Updated Image1", this.Page);
        if (FileUpload1.PostedFile.FileName.Length > 0)
        {
            string imgName = Session["SessionKey"].ToString();
            string imgPath = "../images/Restaurant/" + imgName;
            FileUpload1.SaveAs(Server.MapPath(imgPath));
        }
    }
    if (FileUpload2.PostedFile != null)
    {
        string[] words = FileUpload2.FileName.Split('.');
        string d_name = words[1];
        HttpContext.Current.Session["SessionKey"] = System.Guid.NewGuid().ToString() + "." + d_name;
        cls.UpdateData("_res", new string[] { "res_image2" }, new string[] { Session["SessionKey"].ToString() }, "where res_id='" + Session["ID"].ToString() + "'", "Updated Image2", this.Page);
        if (FileUpload2.PostedFile.FileName.Length > 0)
        {
            string imgName = "2" + Session["SessionKey"].ToString();
            string imgPath = "../images/Restaurant/" + imgName;
            FileUpload2.SaveAs(Server.MapPath(imgPath));
        }
    }
    if (FileUpload3.PostedFile != null)
    {
        string[] words = FileUpload3.FileName.Split('.');
        string d_name = words[1];
        HttpContext.Current.Session["SessionKey"] = System.Guid.NewGuid().ToString() + "." + d_name;
        cls.UpdateData("_res", new string[] { "res_image3" }, new string[] { Session["SessionKey"].ToString() }, "where res_id='" + Session["ID"].ToString() + "'", "Updated Image3", this.Page);
        if (FileUpload3.PostedFile.FileName.Length > 0)
        {
            string imgName = "3" + Session["SessionKey"].ToString();
            string imgPath = "../images/Restaurant/" + imgName;
            FileUpload3.SaveAs(Server.MapPath(imgPath));
        }
    }
    if (FileUpload4.PostedFile != null)
    {
        string[] words = FileUpload4.FileName.Split('.');
        string d_name = words[1];
        HttpContext.Current.Session["SessionKey"] = System.Guid.NewGuid().ToString() + "." + d_name;
        cls.UpdateData("_res", new string[] { "res_image4" }, new string[] { Session["SessionKey"].ToString() }, "where res_id='" + Session["ID"].ToString() + "'", "Updated Image4", this.Page);
        if (FileUpload4.PostedFile.FileName.Length > 0)
        {
            string imgName = "4" + Session["SessionKey"].ToString();
            string imgPath = "../images/Restaurant/" + imgName;
            FileUpload4.SaveAs(Server.MapPath(imgPath));
        }
    }
}

protected void dgnews_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    ListItemType listItemType = e.Item.ItemType;
    if ((listItemType == ListItemType.EditItem) || (listItemType == ListItemType.Item))
    {
        Button imageButton = (Button)e.Item.FindControl("aaa");
        if (imageButton != null)
        {
            UpdatePanel upanel = (UpdatePanel)e.Item.FindControl("UpdatePanel2");
            upanel.Update();
            Image1.ImageUrl = cls.FetchSinglevalue("select res_image from _res where res_id='" + Session["ID"].ToString() + "'");
            Image2.ImageUrl = cls.FetchSinglevalue("select res_image2 from _res where res_id='" + Session["ID"].ToString() + "'");
            Image3.ImageUrl = cls.FetchSinglevalue("select res_image3 from _res where res_id='" + Session["ID"].ToString() + "'");
            Image4.ImageUrl = cls.FetchSinglevalue("select res_image4 from _res where res_id='" + Session["ID"].ToString() + "'");
        }
    }
}
Posted
Updated 17-Aug-12 1:11am
v2

Hi,

I have not checked your whole question as you have posted lengthy code,

But from your question title below thread is similar.

Check this[^] thread answer.

Hope it will help you,

Thanks
-Amit Gajjar
 
Share this answer
 
v2
add the asp control inside trigger in

<asp:PostBackTrigger ControlID="" />
 
Share this answer
 
You must have to refer How to ask a good question?[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900