1

First off, forgive my English, my attempt, I am creating a autocomplete user control, to replace drop downs, I have created the user control, and its working fine. Now for simplicity sake, I need to provide a public property in my User Control to get the selected id, similar to the SelectedValue of the DropDrown control. I'm stuck with this, any ideas will be appreciated.


Hi My Code

UserControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" Code File="UserControl.ascx.cs" Inherits="UserControl" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

function DispValue(sender, e) { alert(e.get_value() + " : user control"); document.getElementById(hiddenFieldName.Client ID).value = e.get_value();
}

UserControl.ascx.cs

public partial class UserControl : System.Web.UI.UserControl {
protected void page_load(object sender, EventArgs e) { ACEName.ContextKey = "1"; }

public String SelectedValue
{
    get { return this.hdnValue.Value; }
}

public String SelectedText
{
    get { return this.Name.Text; }
} }

MyAspxPage.aspx

<%@ Register Src="~/UserControl.ascx" TagPrefix="puc" TagName="UserControl" %>
Patient Name

MyAspxPage.cs DataTable dt; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { dt = new DataTable(); dt.Columns.Add("col1"); dt.Columns.Add("col2"); dt.Columns.Add("col3"); dt.Columns.Add("col4"); dt.Columns.Add("col5"); dt.Columns.Add("col6");

        if (Session["dt"] == null)
        {
            dt = AddRow(dt);
            gvPatient.DataSource = dt;
            gvPatient.DataBind();
            Session["dt"] = dt;
            //ViewState["dt"] = dt;
        }
        else
            dt = (DataTable)Session["dt"];//ViewState["dt"];

    }
}

private DataTable AddRow(DataTable dt)
{
    for (int i = 0; i < 5; i++)
    {
        DataRow dr = dt.NewRow();
        dr[0] = "";
        dr[1] = "";
        dr[2] = "";
        dr[3] = "";
        dr[4] = "";
        dr[5] = "";
        dt.Rows.Add(dr);
    }
    return dt;
}

protected void GridPatient_DataBound(object sender, EventArgs e) { foreach (GridViewRow item in gvPatient.Rows) { UserControl ptuc = (UserControl)item.FindControl("pucPatient1"); string id = ptuc.SelectedValue; } }

public void Save(object sender, EventArgs e) { foreach (GridViewRow item in gvPatient.Rows) { if (item.RowType == DataControlRowType.DataRow) { UserControl ptuc = (UserControl)item.FindControl("pucPatient1"); string id = ptuc.SelectedValue;//getting null value. string patientName = ptuc.SelectedText; } } }

this is all what i did.

Thanking You, cheers Sharanamma.

2 Answers 2

2

Probably you are using the TextBox control in background for your Autocomplete. So, define the SelectedValue as following:

public string SelectedValue
{
   get { return this.textBox.Text; }
}

Or if you need the ID of the selected value, not display text, then place HiddenField near your TextBox and populate the ID of selected value from autocomlete using JavaScript. And the use it on the server side:

public string SelectedValue
{
   get { return this.hiddenField.Text; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

i had one more problem i,e if i expose OnClientItemSelected in autoCompleteextender in usercontrol event as a property means I'm not getting set property of SelectedValue because previously i set hidden field value in onClientItemSelect event of autocompleteextender which is in UserControl and exposed this value in SelectedValue property, so how can i set this property?. any solution means ll be appreciated
0

you can use findcontrol() in gridview's RowDataBound Event. May be it can helps you to find values of hidden field

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.