Id could not be found in Datalist on click of upload button

Viewed 317

I have one page where I am making use of Update panel, Datalist and file upload. I am using visual studio 2010.

My fileupload is in data list and I am binding data list with dynamic table to repeat the file upload control.

Please see below layout image:

enter image description here

Here Main upper red highlighted border is showing the repeated data list and in that I have file upload control.

Now the data list is in Update Panel so the file upload was not working So in data list I have taken another update panel to make file upload work and that was also working properly but on clicking of green add button issue started arising as

A control with ID 'Upload' could not be found for the trigger in UpdatePanel 'UpdatePanel1'.

Below is my html code and please remember I am just giving the part where issue is arises:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="Uppanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:DataList ID="dtcustomerregistration" runat="server" RepeatDirection="Vertical"
            Width="100%" OnItemCommand="dtcustomerregistration_ItemCommand">
            <ItemTemplate>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <label class="col-md-4 control-label" for="Pic">
                            Upload Image:</label>

                        <asp:FileUpload ID="Pic" runat="server" accept="image/gif, image/jpg, image/jpeg, image/png" />

                        <asp:Button ID="Upload" CommandArgument='<%#Eval("uniqueId") %>' CommandName="Edit"
                            runat="server" Text="Upload" OnClick="Upload_Click" />
                        <asp:Label ID="StatusLabel" runat="server" CssClass="requiredvalidate" Text=""></asp:Label>
                        <asp:HiddenField ID="hdimagename" runat="server" Value='<%#Eval("UploadImage") %>' />

                        <asp:Image ID="imgpicuploaded" runat="server" ImageUrl='<%#System.Configuration.ConfigurationManager.AppSettings["ShowImagetemppath"].ToString().Replace("~/","") +Eval("UploadImage").ToString() %>'
                            Height="50px" />
                        </div>
                    </ContentTemplate>
                    <Triggers>
                        <asp:PostBackTrigger ControlID="Upload" />
                    </Triggers>
                </asp:UpdatePanel>
            </ItemTemplate>
        </asp:DataList>
        <asp:ImageButton ID="imgplus" runat="server" ImageUrl="~/Image/Add.png" Height="50px" OnClick="imgplus_Click" />
    </ContentTemplate>
</asp:UpdatePanel>

My code on imgplus click event is as below.

protected void imgplus_Click(object sender, ImageClickEventArgs e)
{
int n = (int)ViewState["n"];
n = n + 1;
BindData(n);
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["data"] = null;
BindData(0);
}
}
public void BindData(int n)
{
DataTable dt = new DataTable();
dt.Columns.Add("uniqueId");
dt.Columns.Add("Invoice");
dt.Columns.Add("ReturnType");
dt.Columns.Add("ItemNumber");
dt.Columns.Add("ReturnQTY");
dt.Columns.Add("UnitofMeasure");
dt.Columns.Add("CreditPer");
dt.Columns.Add("ReasonCode");
dt.Columns.Add("InvoiceNumber");
dt.Columns.Add("Ordernumber");
dt.Columns.Add("Notes");
dt.Columns.Add("UploadImage");

if (ViewState["data"] == null)
{
dt = bindemptydata(dt, n);
}
else
{
foreach (DataListItem dli in dtcustomerregistration.Items)
{
HiddenField lblid = (HiddenField)dli.FindControl("lblid");
DropDownList RetCred = (DropDownList)dli.FindControl("RetCred");
DropDownList returntype = (DropDownList)dli.FindControl("returntype");
TextBox ItemNumber = (TextBox)dli.FindControl("ItemNumber");
TextBox ReturnQty = (TextBox)dli.FindControl("ReturnQty");
DropDownList Unit = (DropDownList)dli.FindControl("Unit");
TextBox Credit = (TextBox)dli.FindControl("Credit");
DropDownList ReasonCode = (DropDownList)dli.FindControl("ReasonCode");
TextBox Invoice = (TextBox)dli.FindControl("Invoice");
TextBox OrderNumber = (TextBox)dli.FindControl("OrderNumber");
TextBox Notes = (TextBox)dli.FindControl("Notes");
Image imgpicuploaded = (Image)dli.FindControl("imgpicuploaded");
HiddenField hdimagename = (HiddenField)dli.FindControl("hdimagename");
DataRow dr = dt.NewRow();

if (lblid.Value != "")
{
dr["uniqueId"] = lblid.Value;
}
else
{
dr["uniqueId"] = n;
}
dr["Invoice"] = RetCred.SelectedValue;
dr["ReturnType"] = returntype.SelectedValue;
dr["ItemNumber"] = ItemNumber.Text;
dr["ReturnQTY"] = ReturnQty.Text;
dr["UnitofMeasure"] = Unit.SelectedValue;
dr["CreditPer"] = Credit.Text;
dr["ReasonCode"] = ReasonCode.SelectedValue;
dr["InvoiceNumber"] = Invoice.Text;
dr["Ordernumber"] = OrderNumber.Text;
dr["Notes"] = Notes.Text;
dr["UploadImage"] = hdimagename.Value;
dt.Rows.Add(dr);
}

dt = bindemptydata(dt, n);
}
BindDatalist(dt);
ViewState["n"] = n;
}

public DataTable bindemptydata(DataTable dt, int n)
{
DataRow dr = dt.NewRow();
dr["uniqueId"] = n;
dr["Invoice"] = "Credit";
dr["ReturnType"] = "0";
dr["ItemNumber"] = "";
dr["ReturnQTY"] = "";
dr["UnitofMeasure"] = "Each";
dr["CreditPer"] = "100%";
dr["ReasonCode"] = "0";
dr["InvoiceNumber"] = "";
dr["Ordernumber"] = "";
dr["Notes"] = "";
dr["UploadImage"] = "";
dt.Rows.Add(dr);

return dt;
}
public void BindDatalist(DataTable dt)
{
dtcustomerregistration.DataSource = dt;
dtcustomerregistration.DataBind();// Here I am receiving error
ViewState["data"] = dt;
}    

Here above I had given full code to create the dynamic data table and binding that with data list so that data list get repeated on click of imgplus button But on click of button I am receiving error as :

Below is the error image. enter image description here

How can I fix this?

On Click on Imgplus button I m receiving issue for upload button which exist in DataList.

And one more thing that if I normally click ImgPlus button without having use of Upload button click then it will repeat the DataList control without giving any issue but in case Once I made use of Upload click button exist in DataList and then after I click Imgplus button I receive error as:

A control with ID 'Upload' could not be found for the trigger in UpdatePanel 'UpdatePanel1'.

2 Answers

Surround your button with another UpdatePanel upButton:

<asp:UpdatePanel ID="upButton" runat="server" UpdateMode="Conditional">
    <asp:Button ID="Upload" CommandArgument='<%#Eval("uniqueId") %>' CommandName="Edit"
        runat="server" Text="Upload" OnClick="Upload_Click" />
</ContentTemplate>

And then call your main UpdatePanel Uppanel.Update(); in your button click method Upload_Click.

Note: Remove UpdatePanel ID="UpdatePanel1".

Related