1
votes

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

2 Answers

2
votes

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; }
}
0
votes

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