下拉列表的选定值 [英] SelectedValue of Dropdown

查看:73
本文介绍了下拉列表的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨-我在页面加载时填充了一个网格视图,在此网格中,每行上都有一个编辑"按钮,单击该按钮可将数据视图中的数据内容复制到GV下方的各种控件(DropdownList,文本框)中.现在,对数据内容所做的任何更改都想保存回数据库并刷新GV. DDL上的DataTextField值显示的是GV中的值,但DataValueField似乎没有更新,因为当我保存对文本框所做的任何更改时,我的代码由于转换而崩溃. ToInt32(ddlHPType.SelectedValue)=".

单击"GV编辑"按钮时,我该怎么做才能为DDL设置DataValueField?

我的DDL填充在这里:

Hi - I''m populating a gridview on the page load, in this grid there is an Edit button on each row which copies the data contents from the gridview into various controls (DropdownLists, textboxes) below the GV once clicked. Now any changes that are made to the data contents I want to save back to the DB and refresh the GV. The DataTextField value on the DDL is displaying the value that was in the GV but the DataValueField doesn''t not seem to be updated because when I got to save any changes I''ve made to the textboxes my code is crashing because Convert.ToInt32(ddlHPType.SelectedValue) = "".

What do i need to do to set the DataValueField for the DDL when the GV Edit button is clicked?

my DDL is populated here:

protected void gvHeatPumpData_SelectedIndexChanged(object sender, EventArgs e)
        {
            //populate textboxes, ddl with values from select row in gvHeatPumpData
            ddlHPRange.SelectedItem.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[2].Text;
            txtHPModelNumber.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[3].Text;
            ddlHPType.SelectedItem.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[4].Text;
            txtHPDimensions.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[5].Text;
            ddlElectricalSupply.SelectedItem.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[6].Text;
            txtHPVoltage.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[7].Text;
            txtHPStartingCurrent.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[8].Text;
            txtHPWeight.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[9].Text;           
        }

推荐答案

您是否在回发中填充控件?如果将其填充到页面加载中,则会重置所选索引.不要使用Convert.ToXXX.使用int.TryParse可以处理无效值.
Are you populating the control in your postback ? if you populate it in your page load, that will reset the selected index. Don''t use Convert.ToXXX. Using int.TryParse so you can deal with invalid values.


每当我用执行的GV行数据填充DDL时:
whenever I was populating the DDL with the GV row data I was doing:
ddlHPRange.SelectedItem.Text = gvHeatPumpData.Rows[gvHeatPumpData.SelectedIndex].Cells[2].Text;



我已将其修改为现在



I''ve amended this to be now

ddlHPRange.SelectedValue = gvHeatPumpData.DataKeys[gvHeatPumpData.SelectedIndex].Values["HeatPumpRangeID"].ToString();


现在,当我保存任何更新时,该ID不为空


and now the ID is not blank when I''m saving any updates


这篇关于下拉列表的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆