I have this RadioButtonList
<asp:RadioButtonList runat="server" ID="loanTypeCheck" CssClass="pull-right" RepeatDirection="Horizontal">
<asp:ListItem Value="Cash" Text=" Cash "/>
<asp:ListItem Value="Non-Cash" Text=" Non-Cash " />
<asp:ListItem Value="Dharauti" Text=" Dharauti " />
</asp:RadioButtonList>
And in Code Behind in my save button event
protected void SaveButton_Click(object sender, EventArgs e)
{
if (IsFormValidForSave())
{
var loanReceipt = GetLoanReceiptFromForm();
var transactionsDetails = new List<TransactionDetail>();
var loantypeCheck = loanTypeCheck.SelectedValue;
//if (IsCashReceipt)
if (loantypeCheck == "Cash")
{
var transaction = new Transaction()
{
AuditUserId = UserId,
OfficeId = OfficeId,
VerificationStatusId = 0,
BookName = EverestEnums.TransactionBookName.LoanReceipt,
BookNameString = EverestEnums.TransactionBookName.LoanReceipt.ToString(),
TranDateString = DateConversion.GetCurrentServerDate().ToString(),
ValueDateString = ValueDateControl.ValueDateAd.ToString(),
TellerId = UserId,
TotalAmount = SubmittedAmountTextBox.Text.ToDecimal(),
ValueDate = ValueDateControl.ValueDateAd,
TranDate = DateConversion.GetCurrentServerDate(),
};
i am getting null value for the radio button.Am i missing something?
Related
I need to create dynamic form, which i will have to create the textbox, textarea,dropdown etc, based on the field type.
Now, I have successfully created this dynamic textbox etc using my rowdatabound.
My problem is I could not retrieve the textbox value entered when the save button is clicked.
My aspx file
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="selectProspect" Checked="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FieldTitle" HeaderText="" />
<asp:BoundField DataField="FieldType" HeaderText="FieldType" />
<asp:TemplateField>
<ItemTemplate>
//DYNAMICALLY ADDING TEXTBOX
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RightText" HeaderText="" />
<asp:BoundField DataField="TemplatesInfoCode" HeaderText="TemplatesInfoCode" ItemStyle-CssClass="hiddencol" />
</Columns>
</asp:GridView>
My code behind file
protected void Page_Load(object sender, EventArgs e)
{
GetTemplateComponent();
}
public void GetTemplateComponent()
{
StringBuilder sb;
DataSet ds;
sb = new StringBuilder();
sb.AppendLine("select * from template where tc='0002' order by Sequence");
ds = Conn.DataAdapter(CommandType.Text, sb.ToString());
gv.DataSource = ds;
gv.DataBind();
}
protected void gv_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string value = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "FieldType"));
if (value == "Date" || value == "TextBox")
{
TextBox TextBox1 = new TextBox() { ID = "TextBox1", EnableViewState = false, CssClass = "form-control" };
e.Row.Cells[3].Controls.Add(TextBox1);
}
if (value == "TextArea")
{
TextBox DateTextBox = new TextBox() { EnableViewState = false, CssClass = "form-control", TextMode = TextBoxMode.MultiLine, Rows = 5, Columns = 50 };
e.Row.Cells[3].Controls.Add(DateTextBox);
}
if (value == "Content")
{
TextBox ContentAreaControl = new TextBox()
{
TextMode = TextBoxMode.MultiLine,
EnableViewState = false,
Columns = 10,
MaxLength = 150,
Height = 200,
CssClass = "Content-container",
};
e.Row.Cells[3].Controls.Add(ContentAreaControl);
}
}
}
protected void SaveTemplateDetails(object sender, EventArgs e)
{
foreach (GridViewRow row in gv.Rows)
{
FieldValue.Value = row.Cells[3].Text.ToString();
*//I DONT KNOW HOW TO READ THE VALUE*
}
}
I just want to know how is the proper way to read this dynamically created textbox
Thank you
ASPX Page Code.
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDataBound="gv_RowDataBound1">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="selectProspect" Checked="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FieldTitle" HeaderText="" />
<asp:BoundField DataField="FieldType" HeaderText="FieldType" />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RightText" HeaderText="" />
<asp:BoundField DataField="TemplatesInfoCode" HeaderText="TemplatesInfoCode" />
</Columns>
</asp:GridView>
Code behind file
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string fieldType = e.Row.Cells[2].Text;
TextBox txtData = e.Row.Cells[3].FindControl("TextBox1") as TextBox;
switch (fieldType)
{
case "Date":
case "TextBox":
txtData.EnableViewState = false;
txtData.CssClass = "form-control";
break;
case "TextArea":
txtData.EnableViewState = false;
txtData.CssClass = "form-control";
txtData.TextMode = TextBoxMode.MultiLine;
txtData.Rows = 5;
txtData.Columns = 50;
break;
case "Content":
txtData.TextMode = TextBoxMode.MultiLine;
txtData.EnableViewState = false;
txtData.Columns = 10;
txtData.MaxLength = 150;
txtData.Height = 200;
txtData.CssClass = "Content-container";
break;
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
SaveTemplateDetails();
}
private void SaveTemplateDetails()
{
foreach (GridViewRow row in gv.Rows)
{
foreach (Control c in row.Cells[3].Controls)
{
TextBox txtDate = c as TextBox;
if (txtDate != null)
{
string data = txtDate.Text;
}
}
}
}
I am creating a quiz application in ASP.net (c#) and I currently have one question at a time displaying with the use of a datapager. However, when i move from question 1 to question 2 and then back to question 1 the radiobutton selection disappears.
EDIT - I now have the radio button retaining a selection whenever I move from next to previous, HOWEVER, If I chose radiobutton1 and then move on to question 2, It has radiobutton1 already selected, even though I am yet to answer that question. So for some reason whatever selection I make on question 1 is being duplicated to the other questions.
Question1 example
Question2 example
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging" OnPreRender="ListPager_PreRender">
<LayoutTemplate>
<div id="itemPlaceHolder1" runat="server">
</div>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="1">
<Fields>
<asp:NextPreviousPagerField runat="server" ButtonType="Link"
ShowFirstPageButton="false"
ShowPreviousPageButton="true"
ShowNextPageButton="false" />
<asp:NumericPagerField ButtonType="Link" />
<asp:NextPreviousPagerField ButtonType="Link"
ShowNextPageButton="true"
ShowLastPageButton="false"
ShowPreviousPageButton="false" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("QuestionID")%>'></asp:Label><br />
<asp:Label ID="Label1" runat="server" Text='<%#Eval("QuestionText")%>'></asp:Label><br />
<li>
<asp:RadioButton ID="Radio1" Text='<%#Eval("Answer1") %>' GroupName="radiobtns" EnableViewState="true" runat="server" />
</li>
<li>
<asp:RadioButton ID="Radio2" runat="server" GroupName="radiobtns" EnableViewState="true" Text='<%#Eval("Answer2") %>' />
</li>
<li>
<asp:RadioButton ID="Radio3" runat="server" GroupName="radiobtns" EnableViewState="true" Text='<%#Eval("Answer3") %>' />
</li>
<li>
<asp:RadioButton ID="Radio4" runat="server" GroupName="radiobtns" EnableViewState="true" Text='<%#Eval("Answer4") %>' />
</li>
<br />
</ItemTemplate>
</asp:ListView>
</div>
<p>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</p>
</form>
</body>
</html>
namespace WebApplication2.WebForms
{
public partial class _1QuestionQuiz : System.Web.UI.Page
{
string userAns;
int QuestionID;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindListView();
if (Session["QuizID"] != null)
{
int QuizID = Convert.ToInt32(Session["QuizID"]);
}
}
}
protected void ListPager_PreRender(object sender, EventArgs e)
{
if (Session["DTSource"] != null)
{
lvCustomers.DataSource = Session["DTSource"];
lvCustomers.DataBind();
}
GetSelections();
}
private void BindListView()
{
SqlConnection conn = new SqlConnection();
string connString = ConfigurationManager.ConnectionStrings["test1ConnectionString"].ConnectionString;
SqlCommand Cmd = new SqlCommand();
conn.ConnectionString = connString;
conn.Open();
if (lvCustomers != null)
{
foreach (ListViewItem item in lvCustomers.Items)
{
Label lbl = (Label)item.FindControl("Label2");
if (lbl != null)
{
QuestionID = Convert.ToInt32(lbl.Text);
ViewState["qstion"] = QuestionID;
}
RadioButton rd1 = (RadioButton)item.FindControl("Radio1");
RadioButton rd2 = (RadioButton)item.FindControl("Radio2");
RadioButton rd3 = (RadioButton)item.FindControl("Radio3");
RadioButton rd4 = (RadioButton)item.FindControl("Radio4");
if (rd1.Checked)
{
userAns = rd1.Text;
ViewState["Radio1"] = rd1.Text;
}
else if (rd2.Checked)
{
userAns = rd2.Text;
ViewState["Radio2"] = rd2.Text;
}
else if (rd3.Checked)
{
userAns = rd3.Text;
ViewState["Radio3"] = rd3.Text;
}
else if (rd4.Checked)
{
userAns = rd4.Text;
ViewState["Radio4"] = rd4.Text;
}
SqlCommand comm = new SqlCommand("InsertSelections", conn);
comm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(comm);
SqlParameter p1 = new SqlParameter("Answer", userAns);
SqlParameter p2 = new SqlParameter("QuestionID", QuestionID);
SqlParameter p3 = new SqlParameter("QuizID", (Session["QuizID"]));
comm.Parameters.Add(p1);
comm.Parameters.Add(p2);
comm.Parameters.Add(p3);
comm.ExecuteNonQuery();
}
}
}
private void GetSelections()
{
foreach (ListViewItem item in lvCustomers.Items)
{
Label lbl = (Label)item.FindControl("Label2");
if (lbl != null)
{
QuestionID = Convert.ToInt32(lbl.Text);
}
RadioButton rd1 = (RadioButton)item.FindControl("Radio1");
RadioButton rd2 = (RadioButton)item.FindControl("Radio2");
RadioButton rd3 = (RadioButton)item.FindControl("Radio3");
RadioButton rd4 = (RadioButton)item.FindControl("Radio4");
//Try radiobutton 1 as a tester
if (rd1 != null)
{
if (lbl != null && ViewState["Radio1"] != null)
{
rd1.Checked = true;
}
}
if (rd2 != null)
{
if (lbl != null && ViewState["Radio2"] != null)
{
rd2.Checked = true;
}
}
if (rd3 != null)
{
if (lbl != null && ViewState["Radio3"] != null)
{
rd3.Checked = true;
}
}
if (rd4 != null)
{
if (lbl != null && ViewState["Radio4"] != null)
{
rd4.Checked = true;
break;
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Score.aspx", false);
}
protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
(lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
BindListView();
}
}
}
Try overriding LoadViewState and SaveViewState to maintain the state of your radio buttons. For example, try something like the following, and pull the values back out of ViewState wherever you need them:
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (ViewState["Radio1"] != null)
Radio1.Text = (int)ViewState["Radio1"];
...
}
protected override object SaveViewState()
{
ViewState["Radio1"] = Radio1.Text;
...
return base.SaveViewState();
}
The issue I am having is I have two groups of radio buttons.
When I click "ALL" in either of them the SharePoint DatePicker Control has to be disabled with a showing custom date (as shown in the image). But as you see the first one got enabled when I clicked on the second group of radiobuttons.
Would appreciate if you help me with this issue.
Thanks.
Below are two groups of radio buttons:
//First Group
<asp:RadioButton ID="Rd4Month" Text="All" AutoPostBack="True" GroupName="GrpDuration" runat="server" OnCheckedChanged="rdgetDateforMonths" />
<asp:RadioButton ID="Rd1Month" Text="1 month" AutoPostBack="True" GroupName="GrpDuration" runat="server" OnCheckedChanged="rdgetDateforMonths" />
<asp:RadioButton ID="Rd2Month" Text="3 month" AutoPostBack="True" GroupName="GrpDuration" runat="server" OnCheckedChanged="rdgetDateforMonths" />
<asp:RadioButton ID="Rd3Month" Text="6 month" AutoPostBack="True" GroupName="GrpDuration" runat="server" OnCheckedChanged="rdgetDateforMonths" />
<asp:RadioButton ID="Rd5Month" Text="Other" AutoPostBack="True" GroupName="GrpDuration" runat="server" OnCheckedChanged="rdgetDateforMonths" />
//Second Group
<asp:RadioButton ID="Rd4BuildMonth" AutoPostBack="True" Text="All" GroupName="GrpBuildDuration" runat="server" OnCheckedChanged="rdgetBuildDateforMonths" />
<asp:RadioButton ID="Rd1BuildMonth" AutoPostBack="True" Text="1 month" GroupName="GrpBuildDuration" runat="server" OnCheckedChanged="rdgetBuildDateforMonths" />
<asp:RadioButton ID="Rd2BuildMonth" AutoPostBack="True" Text="3 month" GroupName="GrpBuildDuration" runat="server" OnCheckedChanged="rdgetBuildDateforMonths" />
<asp:RadioButton ID="Rd3BuildMonth" AutoPostBack="True" Text="6 month" GroupName="GrpBuildDuration" runat="server" OnCheckedChanged="rdgetBuildDateforMonths" />
<asp:RadioButton ID="Rd5BuildMonth" AutoPostBack="True" Text="Other" GroupName="GrpBuildDuration" runat="server" OnCheckedChanged="rdgetBuildDateforMonths" />
//SharePoint DateTimeControl
<table>
<tr>
<td style="vertical-align: middle;">
<asp:Label ID="Label4" runat="server" Text="Start:"></asp:Label>
</td>
<td style="vertical-align: middle;">
<SharePoint:DateTimeControl ID="dtcStartDate" runat="server" DateOnly="True" />
</td>
<td style="vertical-align: middle;">
<asp:Label ID="Label5" runat="server" Text="End:"></asp:Label>
</td>
<td style="vertical-align: middle;">
<SharePoint:DateTimeControl ID="dtcEndDate" runat="server" DateOnly="True" />
</td>
</tr>
</table>
And the codebehind:
protected void rdgetDateforMonths(object sender, EventArgs e)
{
//dtcStartDate.Enabled = true;
// dtcStartDate.ClearSelection();
// ViewState["rd4Month"] = "false";
if (Rd1Month.Checked)
{
dtcStartDate.ClearSelection();
dtcStartDate.Enabled = true;
dtcStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
// Label1.Text = DateTime.Now.AddMonths(-1).ToString();
}
if (Rd2Month.Checked)
{
dtcStartDate.ClearSelection();
dtcStartDate.Enabled = true;
dtcStartDate.SelectedDate = DateTime.Now.AddMonths(-3);
}
if (Rd3Month.Checked)
{
dtcStartDate.ClearSelection();
dtcStartDate.Enabled = true;
dtcStartDate.SelectedDate = DateTime.Now.AddMonths(-6);
}
if (Rd4Month.Checked)
{
dtcStartDate.ClearSelection();
DateTime value = new DateTime(2012, 04, 01);
dtcStartDate.SelectedDate = value;
dtcStartDate.Enabled = false;
// ViewState["rd4Month"] = "true";
}
if (Rd5Month.Checked)
{
dtcStartDate.Enabled = true;
dtcStartDate.ClearSelection();
}
}
protected void rdgetBuildDateforMonths(object sender, EventArgs e)
{
// dtcBuildStartDate.ClearSelection();
// dtcBuildStartDate.Enabled = true;
// ViewState["rd4BuildMonth"] = "false";
if (Rd1BuildMonth.Checked)
{
dtcBuildStartDate.Enabled = true;
dtcBuildStartDate.ClearSelection();
dtcBuildStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
}
if (Rd2BuildMonth.Checked)
{
dtcBuildStartDate.ClearSelection();
dtcBuildStartDate.Enabled = true;
dtcBuildStartDate.SelectedDate = DateTime.Now.AddMonths(-3);
}
if (Rd3BuildMonth.Checked)
{
dtcBuildStartDate.Enabled = true;
dtcBuildStartDate.ClearSelection();
dtcBuildStartDate.SelectedDate = DateTime.Now.AddMonths(-6);
}
if (Rd4BuildMonth.Checked)
{
dtcBuildStartDate.Enabled = false;
dtcBuildStartDate.ClearSelection();
DateTime value = new DateTime(2012, 04, 01);
dtcBuildStartDate.SelectedDate = value;
dtcBuildStartDate.Enabled = false;
// ViewState["rd4BuildMonth"] = "true";
}
if (Rd5BuildMonth.Checked)
{
dtcBuildStartDate.Enabled = true;
dtcBuildStartDate.ClearSelection();
}
}
protected void Page_Load(object sender, EventArgs e)
{
//dtcStartDate.MaxDate = System.DateTime.Today;
dtcBuildStartDate.MaxDate = DateTime.Now.AddMonths(-1);
dtcStartDate.MaxDate = DateTime.Now.AddMonths(-1);
dtcEndDate.MaxDate = System.DateTime.Today;
dtcBuildEndDate.MaxDate = System.DateTime.Today; ;
lblErrorMsg.Text = "";
// When the page loads 1st time
if (!Page.IsPostBack)
{
try
{
Rd1Month.Checked = true;
Rd1BuildMonth.Checked = true;
dtcEndDate.SelectedDate = System.DateTime.Today; // set end date calendar to today's date
dtcBuildEndDate.SelectedDate = System.DateTime.Today;
dtcStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
dtcBuildStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
}
catch (Exception ex)
{
lblErrorMsg.Text += ex.Message;
}
}
}
After I modified the Enable for DateTimeControl to False
<SharePoint:DateTimeControl ID="dtcBuildStartDate" runat="server" DateOnly="True" Enabled="False" />
After reviewing the Page_Load method that you just added and reading the comment to my previous post. It looks like the problem is with the way the share point date time control acts. You can try adding EnableViewState="true".
<SharePoint:DateTimeControl ID="dtcBuildStartDate" runat="server" DateOnly="True" Enabled="False" EnableViewState="true" />
If viewstate is not turned on then every time your page posts back it reloads the default value from the aspx page definition. This would also affect things like the control's value not being maintained after a postback etc. If the enableviewstate doesn't work then you could try adding storing the state yourself in ViewState then on each postback you would have to manage this value, but it might get complicated with dealing with changes etc.
You can also try the following on page load:
protected void Page_Load(object sender, EventArgs e)
{
//dtcStartDate.MaxDate = System.DateTime.Today;
dtcBuildStartDate.MaxDate = DateTime.Now.AddMonths(-1);
dtcStartDate.MaxDate = DateTime.Now.AddMonths(-1);
dtcEndDate.MaxDate = System.DateTime.Today;
dtcBuildEndDate.MaxDate = System.DateTime.Today; ;
lblErrorMsg.Text = "";
// When the page loads 1st time
if (!Page.IsPostBack)
{
try
{
Rd1Month.Checked = true;
Rd1BuildMonth.Checked = true;
dtcEndDate.SelectedDate = System.DateTime.Today; // set end date calendar to today's date
dtcBuildEndDate.SelectedDate = System.DateTime.Today;
dtcStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
dtcBuildStartDate.SelectedDate = DateTime.Now.AddMonths(-1);
}
catch (Exception ex)
{
lblErrorMsg.Text += ex.Message;
}
}
SetDateBoxEnabled();
}
private void SetDateboxEnabled()
{
if (Rd4BuildMonth.Checked)
{
dtcBuildStartDate.Enabled = false;
}
else
{
dtcBuildStartDate.Enabled = true;
}
if (Rd4Month.Checked)
{
dtcStartDate.Enabled = false;
}
else
{
dtcStartDate.Enabled = true;
}
}
I'm using radlistview in my asp.net project . The problem that when I have to get all items of radlistview it return only items of the current page.
Example :
radlistview2.Items.ToList();
Output :
I got only 10 elements which are in the first page
My Question is :
How To get all items in radlistview ???
Please try with the below code snippet.
ASPX
<telerik:RadListView ID="RadListView1" runat="server" OnNeedDataSource="RadListView1_NeedDataSource" AllowPaging="true" PageSize="2">
<ItemTemplate>
<%# Eval("ID") %>
</ItemTemplate>
</telerik:RadListView>
<telerik:RadDataPager ID="RadDataPager1" runat="server" PagedControlID="RadListView1"
PageSize="2">
<Fields>
<telerik:RadDataPagerButtonField FieldType="FirstPrev"></telerik:RadDataPagerButtonField>
<telerik:RadDataPagerButtonField FieldType="Numeric"></telerik:RadDataPagerButtonField>
<telerik:RadDataPagerButtonField FieldType="NextLast"></telerik:RadDataPagerButtonField>
</Fields>
</telerik:RadDataPager>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
ASPX.CS
protected void RadListView1_NeedDataSource(object sender, RadListViewNeedDataSourceEventArgs e)
{
dynamic data1 = new[] {
new { ID = 1, Name ="Name_1",Customdate = DateTime.Now},
new { ID = 2, Name = "Name_2",Customdate = DateTime.Now},
new { ID = 3, Name = "Name_3",Customdate = DateTime.Now},
new { ID = 4, Name = "Name_4",Customdate = DateTime.Now},
new { ID = 5, Name = "Name_5",Customdate = DateTime.Now}
};
RadListView1.DataSource = data1;
}
protected void Button1_Click(object sender, EventArgs e)
{
int i = RadListView1.Items.Count();
RadListView1.AllowPaging = false;
RadListView1.Rebind();
int j = RadListView1.Items.Count();
//Access your count here
RadListView1.AllowPaging = true;
RadListView1.Rebind();
}
I have a radgrid and a javascript in an ASP page.
This is the script:
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script type="text/javascript">
function ViewCheck(filename) {
var targetfile = "Allegati/"+ filename;
var openWnd = radopen(targetfile, "RadWindowDetails");
}
</script>
The above script is supposed to pass the path of a file to a Radwindow and is working fine.
My problem is that for various reasons, I now need now to create subfolders of "Allegati" to store the files separately for each record. Such subfolders are named with the recordID value.
So now the var "targetfile" should be:
var targetfile = "Allegati/"+ recordID + filename;
In code behind, I get the recordID as following:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
string recordID = editedItem.GetDataKeyValue("TransazioneID").ToString();
}
}
How can I pass the subfolder value "recordID" to the javascript in the asp page to get the complete path of the folder containing the files?
Please try with the below code snippet. Let me know if any concern.
JS
<script type="text/javascript">
function OPenPopuP(id, name, serverPath) {
var targetfile = serverPath + "/Allegati/" + id + "/" + name;
var openWnd = radopen(targetfile, "RadWindowDetails");
return false;
}
</script>
ASPX
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<telerik:RadWindowManager ID="RadWindowManager1" runat="server">
</telerik:RadWindowManager>
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
OnItemDataBound="RadGrid1_ItemDataBound">
<MasterTableView CommandItemDisplay="Top" DataKeyNames="ID,FileName">
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="Button1" runat="server" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridEditCommandColumn>
</telerik:GridEditCommandColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
ASPX.CS
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data1 = new[] {
new { ID = 1, Name ="Name_1",FileName = "jayesh-Softweb.jpg"},
new { ID = 2, Name = "Name_2",FileName = "jayesh-Softweb.jpg"},
new { ID = 3, Name = "Name_3",FileName = "jayesh-Softweb.jpg"},
new { ID = 4, Name = "Name_4",FileName = "jayesh-Softweb.jpg"},
new { ID = 5, Name = "Name_5",FileName = "jayesh-Softweb.jpg"}
};
RadGrid1.DataSource = data1;
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = e.Item as GridDataItem;
string strID = item.GetDataKeyValue("ID").ToString();
string strFileName = item.GetDataKeyValue("FileName").ToString();
Button Button1 = item.FindControl("Button1") as Button;
Button1.Attributes.Add("onclick", "return OPenPopuP('" + strID + "','" + strFileName + "','" + Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath + "');");
}
else if (e.Item.IsInEditMode && e.Item is GridEditableItem)
{
GridEditableItem item = e.Item as GridEditableItem;
string strID = item.GetDataKeyValue("ID").ToString();
string strFileName = item.GetDataKeyValue("FileName").ToString();
Button Button1 = item.FindControl("Button1") as Button;
Button1.Attributes.Add("onclick", "return OPenPopuP('" + strID + "','" + strFileName + "','" + Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath + "');");
}
}
Add a hidden field to your aspx page.
<asp:HiddenField ID="hfrecordID" runat="server" />
And assign the recodId to it in the ItemDataBound event and use it in the aspx page.
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script type="text/javascript">
function ViewCheck(filename) {
var targetfile = "Allegati/" + <%= hfrecordID.value %> + filename;
var openWnd = radopen(targetfile, "RadWindowDetails");
}
</script>
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
hfrecordID= editedItem.GetDataKeyValue("TransazioneID").ToString();
}
}