Critical Developers

Programmers Knowledge Base

Access Repeater's First Item / nth Item / Item with ItemIndex Without ItemDataBound / OutSide of Repeater

// Get repeater First Item Without ItemDataBound || OutSide of Repeater

// Repeater ? := RepeaterItemCollection(Items) =>  RepeaterItem(Single Item) => DataItem

// Note :- We can access the DataItem only in OnItemDataBound Event else Use item.FindControl("ID")

Access Nth Item/ DataItem using ItemIndex:-

            RepeaterItemCollection loItemCollection = (RepeaterItemCollection)rptProduct.Items;
            RepeaterItem rptItem = (RepeaterItem)loItemCollection[0];  // First Item i.e. Index = 0
            Label lblProduct= (Label)rptItem.FindControl("lblProduct");
            Label lblCategory= (Label)rptItem.FindControl("lblCategory");
            Label lblAmount = (Label)rptItem.FindControl("lblAmount");


Access All DataItem :-

protected void txtAmount_TextChanged(object sender, EventArgs e)
   {        
        foreach (RepeaterItem rptItem in rptProduct.Items)
        {
            Label lblABC = (Label)rptItem.FindControl("lblABC");
            if (!string.IsNullOrEmpty(txtAmount.Text.Trim()) && txtAmount.Text.Trim() != "")
                {
                //  your code block;
                }
        }
   }


Access Single Item if control is inside Repeater :-

protected void txtAmount_TextChanged(object sender, EventArgs e)
    {
        TextBox txtAmount = (TextBox)sender;
        RepeaterItem rptItem = (RepeaterItem)txtAmount.NamingContainer;
       
Label lblABC = (Label)rptItem.FindControl("lblABC");       
        if (!string.IsNullOrEmpty(txtAmount.Text.Trim()) && txtAmount.Text.Trim() != "")
        {
          
                     //  your code block.
        }
    }


====[ END ]=========

Confirm OnTextChanged of TextBox with javascript(if Cancel Restore the previous value, if Ok Call Server Side Script) - ASP.Net

Lets the TextBox is in ListView or any MultiDimensional DataBound Control so that the TextBox have some default value first time its load.

TextBox HTMLization :- (Recommend to use UpdatePanel to ajaxify)

<asp:TextBox ID="txtProdAmount" runat="server" Width="80" Text='<%#Eval("product_amt", "{0:0.00}")%>' AutoPostBack="true"
ontextchanged="txtProdAmount_TextChanged"
onchange="if (!confirm('Are U Sure to Change the Amount  ?')) { this.value=this.defaultValue; return false; }" EnableViewState="true">
</asp:TextBox>

AutoPostBack="true"  // To Call the ServerSide || CodeBehind TextChanged Event.

ontextchanged="txtProdAmount_TextChanged"  // Call ServerSide TextChanged Event once confirm from client.

onchange=""   // Call ClientSide TextChanged Event

Javascript:-

if (!confirm('Are U Sure to Change the Amount  ?'))  //  on Confirm if action is "Cancel"

{ this.value=this.defaultValue; return false; }   //  then restore its previous value in textbox


Code Behind:-

  protected void txtProdAmount_TextChanged(object sender, EventArgs e)
    {
        TextBox txtProdAmount= (TextBox)sender;
        RepeaterItem rptItem = (RepeaterItem)txtProdAmount.NamingContainer;
        HiddenField hdnProdID = (HiddenField)rptItem.FindControl("hdnProdID");
       
        if (!string.IsNullOrEmpty(txtProdAmount.Text.Trim()) && txtProdAmount.Text.Trim() != "")
        {
            int liSuccess = 0;
            decimal? ldProdID = null;
            decimal? ldProdAmount = null;
            ldProdID = Convert.ToDecimal(hdnProdID.Value);
            ldProdAmount = Convert.ToDecimal(txtProdAmount.Text);
            Product loProductObj = new Product();
            liSuccess = loProductObj.UpdateProductAmount(ldProdID , ldProdAmount ); // Call Update Procedure
            if (liSuccess > 0)
            {
                txtProdAmount.Text = Convert.ToDecimal(txtProdAmount.Text).ToString("0.00");  // To re-Format the TextBox
                Common.ShowAlert("Product Amount Updated.", this); // Alert if Updated
            }
        }

    }

===[ END ]=========