So I am new to using ASP.net and i have a hard time displaying the value/text of the selected row inside the dropdown. I just want to select a row in the griview and when i select, the values in it should populate the textboxes and dropdown, can someone help me ? thanks
this is my code behind, as you can see in the LoadData() part I used the .findtext() but it outputs an error :Object reference not set to an instance of an object.
protected void grdRecentCases_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';this.style.cursor='Pointer'";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdRecentCases, "Select$" + e.Row.RowIndex);
}
}
protected void grdRecentCases_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
LoadData(Convert.ToInt32(e.CommandArgument));
}
}
///<summary> LoadData is used to populate inputboxes with the value of the selected griview row</summary>
///<param name="rowNumber"> Indicates whether there is a selected row or non</param>
private void LoadData(int? rowNumber = null)
{
//if rowNumber is null use GridView1.SelectedIndex
var index = rowNumber ?? grdRecentCases.SelectedIndex;
//Populate the input box with the value of selected row.
GridViewRow gr = grdRecentCases.Rows[index];
txtDepartmentCase.Text = gr.Cells[2].Text;
txtLabCase.Text = gr.Cells[5].Text;
txtIncidentReportDate.Text = gr.Cells[6].Text;
txtCaseKey.Text = gr.Cells[1].Text;
drpDepartment.Items.FindByText(gr.Cells[3].Text).Selected = true;
drpCharge.Items.FindByText(gr.Cells[4].Text).Selected = true;
}
Html code
<table class="style2">
<tr>
<td class="style3">
Department Case #
</td>
<td>
<asp:TextBox ID="txtDepartmentCase" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Department
</td>
<td>
<asp:DropDownList ID="drpDepartment" runat="server" Height="18px" Width="153px" Enabled="False"
AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="drpDepartment_SelectedIndexChanged"
Visible="true">
</asp:DropDownList>
<asp:TextBox ID="txtDepartment" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Charge
</td>
<td>
<asp:DropDownList ID="drpCharge" runat="server" Height="22px" Width="153px" Enabled="False"
AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="drpCharge_SelectedIndexChanged"
Visible="true">
</asp:DropDownList>
<asp:TextBox ID="txtCharge" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Lab Case #
</td>
<td>
<asp:TextBox ID="txtLabCase" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Incident Report Date
</td>
<td>
<asp:TextBox ID="txtIncidentReportDate" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
</table>
As you mentioned, to select a row in the griview and when i select, the values in it should populate the textboxes and dropdown.
Hope this can help you.
Create a datagridview event, RowHeaderMouseClick
private void datagridview_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
string selectedDoc = datagridview.Rows[datagridview.SelectedRows[0].Index].Cells["column name"].Value.ToString() ;
combobox.Text = selectedDoc;
}
Related
I have the following mark up for a User Control (.ascx)
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Select Logical Symbol to Search:"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlComponentType" runat="server"
onselectedindexchanged="ddlComponentType_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
</td>
<td>
<asp:CheckBox ID="chkAdvSearchAllLibs" runat="server" ToolTip="Check this box to search all available libraries" Text="Search All Libraries"/>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Search by Logical Symbol Properties:"></asp:Label>
</td>
<td>
</td>
</tr>
On page Load
protected void Page_Load(object sender, EventArgs e)
{
SearchResults(ref attributeSearch, compTypeID);
}
where SearchResults is
private void SearchResults(ref string attributeSearch, int compTypeID)
{
DataTable dtResults = this.AdvancedSearchControl.GetSearchResults(ref attributeSearch, compTypeID);
}
And in my UserControl.ascx.cs
public DataTable GetSearchResults(ref string _attrVals, int compTypeID)
{
//Other Logic Goes Here
IEnumerable<Model.ComponentInfo.ComponentType> compTypeResult = from compTypes in BLLibrary.GetComponentTypeBasedOnLib(this.CurrentLibraryId, this.CurrentLibrary, this.chkAdvSearchAllLibs.Checked) select compTypes.Value;
}
this.chkAdvSearchAllLibs.Checked is always false no matter if the check-box is checked on page and posted back or not.
Server side:
Add AutoPostBack="True" to the CheckBox. It's not posting back.
Client side:
<asp:CheckBox runat="server" ID="cb" onclick="checkboxchanged(this);" />
function checkboxchanged( sender ) {
if ( sender.checked ) {
// clicked and checked
} else {
// clicked and unchecked
}
}
I have a repeater control. In OnItemCommand method of repeater, after doing some operations I am trying to refresh the repeater list so that it could show me the updated list. But somehow repeater list is not updated in method. However when I reload the page then repeater list gets updated with most recent items.
ASPX:
<asp:Label ID="Message1" runat="server" ForeColor="Blue" Text=""></asp:Label>
<div id="ListingAgentsData" class="panel panel-default" style="width: 920px;">
<asp:Repeater ID="rptagentList" runat="server" OnItemCommand="rptagentList_OnItemCommand">
<HeaderTemplate>
<table id="results1" cellpadding="4" cellspacing="1" width="100%">
<tr>
<td>
<strong>AgentID</strong>
</td>
<td>
<strong>Email</strong>
</td>
<td>
<strong>FullName</strong>
</td>
<td>
<strong>Driver License/Passport</strong>
</td>
<td>
<strong>Action</strong>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblDayofweek" runat="server" Text='<%#Eval("AgentID")%>'></asp:Label>
</td>
<td>
<asp:Label ID="lblTime" runat="server" Text='<%#Eval("Email")%>'></asp:Label>
</td>
<td>
<asp:Label ID="lblCharges" runat="server" Text='<%#Eval("FullName")%>'></asp:Label>
</td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("DriverLicense")%>'></asp:Label>
</td>
<td>
<asp:LinkButton ID="ibtn" runat="server" Text="Approve" CommandName="Approve" CommandArgument='<%#Eval("AgentID")%>' />
<asp:LinkButton ID="LinkButton1" runat="server" Text="Reject" CommandName="Reject" CommandArgument='<%#Eval("AgentID")%>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
.CS:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Username"] == null)
Response.Redirect("../Login/Default.aspx");
else
{
Init();
}
}
protected void rptagentList_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
try
{
if (e.CommandName == "Approve")
{
int agentId = Convert.ToInt32(e.CommandArgument);
GM gm = new GM();
if (gm.ApproveListingAgent(agentId))
{
Message1.Text = "Listing Agent with ID:"+agentId+" is approved!";
Init();
}
}
else if (e.CommandName == "Reject")
{
}
}
catch (Exception ex)
{
throw ex;
}
}
private void Init()
{
ListingAgent Agent = new ListingAgent();
DataTable dt = Agent.getPendingListingAgents();
if (dt.Rows.Count > 0)
{
rptagentList.DataSource = dt;
rptagentList.DataBind();
}
}
What am I missing?
Please help!
I have a page called info.aspx where a user inputs his/her information. After the information entered it goes to the next page called confirm.aspx where a user can confirm details or click on an edit button an it will take them back to the info.aspx where they can edit the information.
Once I click the Edit button it will prefill the values from the session variables. But the problem is that if I change the Last Name in the textbox and it goes to the next page it will not update, it keeps the old value. I debug and saw that tbLastName for example keeps the same Last Name even tho I change it. Am I missing something here?
I have this stored in Session Variables.
info.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Session["FirstName"] != null)
{
tbFirstName.Text = Session["FirstName"].ToString();
tbLastName.Text = Session["LastName"].ToString();
tbDOB.Text = Session["DOB"].ToString();
tbEmail.Text = Session["Email"].ToString();
tbTelephone.Text = Session["Telephone"].ToString();
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
Session["FirstName"] = tbFirstName.Text;
Session["LastName"] = tbLastName.Text;
Session["DOB"] = tbDOB.Text;
Session["Email"] = tbEmail.Text;
Session["Telephone"] = tbTelephone.Text;
Response.Redirect("~/appointments/confirm.aspx");
}
info.aspx
<table class="warning" style="width: inherit;">
<tr>
<td>
First Name:
</td>
<td>
<asp:TextBox ID="tbFirstName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator2" ControlToValidate="tbFirstName"
Text="*" ForeColor="Yellow" ValidationGroup="InsertInformation" />
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<asp:TextBox ID="tbLastName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator3" ControlToValidate="tbLastName"
Text="*" ForeColor="Yellow" ValidationGroup="InsertInformation" />
</td>
</tr>
<tr>
<td>
Date of Birth:
</td>
<td>
<asp:TextBox ID="tbDOB" runat="server"></asp:TextBox>
<ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender1" runat="server" Mask="99/99/9999"
AcceptNegative="None" MessageValidatorTip="true" TargetControlID="tbDOB" ClearMaskOnLostFocus="false"
ClearTextOnInvalid="false" MaskType="Date" />
<ajaxToolkit:MaskedEditValidator ID="MaskedEditValidator5" runat="server" ControlExtender="MaskedEditExtender1"
ControlToValidate="tbDOB" Display="Dynamic" ForeColor="Yellow" EmptyValueBlurredText="*"
InvalidValueBlurredMessage="*" ValidationGroup="InsertInformation" />
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<asp:TextBox ID="tbEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" ControlToValidate="tbEmail"
Text="*" ForeColor="Yellow" ValidationGroup="InsertInformation" />
</td>
</tr>
<tr>
<td>
Telephone:
</td>
<td>
<asp:TextBox ID="tbTelephone" runat="server"></asp:TextBox>
<ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender" runat="server" Mask="(999)999-9999"
AcceptNegative="None" MessageValidatorTip="true" TargetControlID="tbTelephone"
MaskType="Number" ClearMaskOnLostFocus="false" ClearTextOnInvalid="false" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnNext" runat="server" Text="Next" OnClick="btnNext_Click" ValidationGroup="InsertInformation" />
</td>
</tr>
confirm.aspx.cs
protected void btnEditInfo_Click(object sender, EventArgs e)
{
Response.Redirect("/info.aspx");
}
It's rebinding the text boxes when you post back.
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["FirstName"] != null && !IsPostback)
{
tbFirstName.Text = Session["FirstName"].ToString();
tbLastName.Text = Session["LastName"].ToString();
tbDOB.Text = Session["DOB"].ToString();
tbEmail.Text = Session["Email"].ToString();
tbTelephone.Text = Session["Telephone"].ToString();
}
}
When clicking on BtnNext, Page_Load event of info.aspx.cs is invoked before the Redirect. You just need to check !IsPostBack property in Page_Load of info.aspx.cs, just like jasonwarford mentioned. That way the textbox is not reset:
if (!IsPostBack)
{
tbFirstName.Text = Session["FirstName"].ToString();
tbLastName.Text = Session["LastName"].ToString();
tbDOB.Text = Session["DOB"].ToString();
tbEmail.Text = Session["Email"].ToString();
tbTelephone.Text = Session["Telephone"].ToString();
}
<asp:DropDownList ID="ddlGroupSearch" CssClass="cssTextbox" runat="server">
<asp:ListItem Value="1" Text="Referring Physician Wise"></asp:ListItem>
<asp:ListItem Value="2" Text="Specialty Wise"></asp:ListItem>
</asp:DropDownList>
<asp:Repeater runat="server" ID="repeaterParent" OnItemDataBound="repeaterParent_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblREF_PHY_ID" runat="server" Text='<%# this.RenderGroup(Eval("REF_PHY_ID") as string)%>'></asp:Label>
</td>
</tr>
<tr>
<td class="cssTableListTd">
<asp:Label ID="lblPatNameListData" runat="server" Text='<%#Eval("Patient_Name")%>'></asp:Label>
</td>
<td class="cssTableListTd">
<asp:Label ID="lblPatIDListData" runat="server" Text='<%#Eval("Patient_ID")%>'></asp:Label>
</td>
<td class="cssTableListTd">
<asp:Label ID="lblPatSexListData" runat="server" Text='<%#Eval("Sex")%>'></asp:Label>
</td>
<td class="cssTableListTd">
<asp:Label ID="lblPatBirthDateListData" runat="server" Text='<%#Eval("Patients_Birth_Date")%>'></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
c#
protected void Page_Load(object sender, EventArgs e)
{
//get data bind to repeater
repeaterParent.DataSource = DataTable;
repeaterParent.DataBind();.
}
string lastValue = "";
protected string RenderGroup(string currentValue)
{
if(currentValue == this.lastValue)
return "";//Group has changed
this.lastValue = currentValue;
return currentValue;
}
I have to show report group wise. I have a DropDownList in my page. Now according to the DropDownList value I have to call RenderGroup function. How should I call in repeater?
I have taken a reference of this link
ref link
Did you tried by applying group by functionality in to DataTable that you are binding to repeater?
I am pointing to something like below.
Ref: Link
Thanks!
i have two tables which stores items and itemsizes, a item will have different sizes. i am trying to display the itemsizes for the item in a dropdownlist with in a datalist. my html is as below
<asp:DataList ID="dlstCartItems" runat="server" RepeatDirection="Horizontal" RepeatColumns="5"
Width="580px" ForeColor="Blue" DataKeys="ItemCode"
onitemdatabound="dlstCartItems_ItemDataBound">
<ItemTemplate>
<table cellpadding="0" cellspacing="0" style="border: Solid 2px #eeeeee;">
<tr>
<td align="left" style="width: 180px;">
<a href="Videos.aspx?vid=<%# Eval("itemid") %>">
<img src="images/Gallery/<%# Eval("imagename") %>" alt="Image" height="120px" width="185px"
style="border: 0;" />
</a>
</td>
</tr>
<tr>
<td style="width: 180px;" align="left">
<table>
<tr>
<td>
Name:
</td>
<td>
<a href="Videos.aspx?vid=<%# Eval("itemid") %>">
<asp:Label ID="lblVideoName" runat="server" Text='<%# Eval("name")%>' ForeColor="#06C"></asp:Label>
</a>
</td>
</tr>
<tr>
<td>
Author:
</td>
<td>
<a href="Videos.aspx?Authorid=<%# Eval("itemid") %>">
<asp:Label ID="Label1" runat="server" Text='<%# Eval("thm") %>' ForeColor="#06C"></asp:Label></a>
</td>
</tr>
<tr>
<td>
Technology:
</td>
<td>
<a href="Videos.aspx?Techid=<%# Eval("itemid") %>">
<asp:Label ID="Label2" runat="server" Text='<%# Eval("itemcode") %>' ForeColor="#06C"></asp:Label></a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="ddlAvailableSizes" runat="server" >
</asp:DropDownList>
</td>
</tr>
</table>
</ItemTemplate>
<%--<AlternatingItemStyle BackColor="Silver" />--%>
<ItemStyle BackColor="#eeeeee" />
</asp:DataList>
This is my bind method
var query = (from b in db.CR_CartItems
join c in db.CR_CartItems_Sizes on b.ItemCode equals c.ItemCode
join k in db.CR_ScrollingMenus on b.ThemeID equals k.MenuID
where b.status == true
orderby b.updatedat descending
select new
{
itemid = b.Itemid,
imagename = b.galleryimg,
itemcode = b.ItemCode,
thm = k.DisplayName,
name = b.posterName
}).Distinct();
foreach (var q in query)
{
var query1 = from b in db.CR_CartItems_Sizes
join c in db.CR_CartItems on b.ItemCode equals c.ItemCode
where b.ItemCode == q.itemcode
select new
{
b.SizeDesc,
b.ItemCode
};
foreach (DataListItem li in dlstCartItems.Items)
{
DropDownList list = (DropDownList)li.FindControl("ddlAvailableSizes");
list.DataSource = query1;
list.DataTextField = "SizeDesc";
list.DataValueField = "ItemCode";
list.DataBind();
list.Items.Insert(0, "Available Sizes");
}
}
dlstCartItems.DataSource = query;
dlstCartItems.DataBind();
}
i have set up a break point at the foreach loop and checked, it is not entering the loop. any help is appreciated.
You have to handle the DataList.ItemDataBound event:
Assuming you are binding your DataList like: (or similar, using a data source control)
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.dl.DataSource = GetDataSource();
this.dl.DataBind();
}
}
Then your ItemDataBound handler would look like:
protected void dl_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var myDropDownList = e.Item.FindControl("YourDropDownListID") as DropDownList;
int currentItemID = int.Parse(this.dl.DataKeys[e.Item.ItemIndex].ToString());
myDropDownList.DataSource = GetDDLDataSource(currentItemID);
myDropDownList.DataBind();
}
}
Finally make sure to register the event in the DataList markup
<asp:DataList ID="dl" runat="server"
onitemdatabound="dl_ItemDataBound"
DataKeyField="Your_Row_ID"
>